Last active
May 23, 2016 17:03
-
-
Save proclaim/d8b190b69ef7cdce5dc9395b3c6db553 to your computer and use it in GitHub Desktop.
Flatten Nested Array ES6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @summary function that will flatten nested array into regular array | |
* @param Array - nested array (can be in different level) | |
*/ | |
const flatten = nestedArray => nestedArray.reduce( | |
(a, next) => a.concat(Array.isArray(next) ? flatten(next) : next), [] | |
); | |
// Test | |
var numbers = [[1,2,[3]],4,[5,[6,[7,8]]]]; | |
console.log(flatten(numbers)); // [1, 2, 3, 4, 5, 6, 7, 8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment