Guest User

Untitled

a guest
Oct 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. /**
  2. * Flatten any combination of nested ints/arrays
  3. * in an array recursively into a flat array
  4. * @param input {Array}
  5. * @returns {Array}
  6. */
  7. function flatten(input) {
  8. let output = [];
  9.  
  10. for(let i = 0; i < input.length; i++) {
  11. const item = input[i];
  12.  
  13. // recursively add arrays
  14. if(typeof item == 'object' && Array.isArray(item) && item.length) {
  15. output.push(...flatten(item));
  16. }
  17.  
  18. // add simple number instances
  19. if(typeof item == 'number') {
  20. output.push(item);
  21. }
  22. }
  23.  
  24. return output;
  25. }
  26.  
  27. module.exports = flatten;
Add Comment
Please, Sign In to add comment