Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. /**
  2. *A function that takes a (nested) array as input and returns a flattened version of it
  3. *@param {Array} input - arbitrarily nested array of integers
  4. *@param {Array} input - progressively built flattened output array
  5. *@returns {Array} the flattened array, empty array if the input is either not an array or an empty array
  6. */
  7. var flatten = function(input, output) {
  8. if (!input || !input.length) return [];
  9. output = output || [];
  10. for (var i = 0; i < input.length; i++) {
  11. var curr = input[i];
  12. if (Array.isArray (curr)) {
  13. flatten(curr, output);
  14. }
  15. else {
  16. output[output.length++] = curr;
  17. }
  18. }
  19. return output;
  20. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement