Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // recursive method
- function flattenArray(arr) {
- var newArr = [],
- i = 0,
- arrLen = arr.length;
- for (i; i < arrLen; i++) {
- if (Array.isArray(arr[i])) {
- newArr = newArr.concat(flattenArray(arr[i]));
- } else {
- newArr.push(arr[i]);
- }
- }
- return newArr;
- }
- // ES6 reduce method
- const flattenArrayNew = (arr) => arr.reduce(
- (a, b) => a.concat(
- Array.isArray(b) ? flattenArrayNew(b) : b
- ),
- []
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement