Skip to content

Instantly share code, notes, and snippets.

@proclaim
Last active May 23, 2016 17:03
Show Gist options
  • Save proclaim/d8b190b69ef7cdce5dc9395b3c6db553 to your computer and use it in GitHub Desktop.
Save proclaim/d8b190b69ef7cdce5dc9395b3c6db553 to your computer and use it in GitHub Desktop.
Flatten Nested Array ES6
/**
* @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