Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. // recursive method
  2. function flattenArray(arr) {
  3. var newArr = [],
  4. i = 0,
  5. arrLen = arr.length;
  6.  
  7. for (i; i < arrLen; i++) {
  8. if (Array.isArray(arr[i])) {
  9. newArr = newArr.concat(flattenArray(arr[i]));
  10. } else {
  11. newArr.push(arr[i]);
  12. }
  13. }
  14. return newArr;
  15. }
  16.  
  17. // ES6 reduce method
  18. const flattenArrayNew = (arr) => arr.reduce(
  19. (a, b) => a.concat(
  20. Array.isArray(b) ? flattenArrayNew(b) : b
  21. ),
  22. []
  23. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement