Skip to content

Instantly share code, notes, and snippets.

@kra3
Created December 13, 2016 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kra3/f802061437e3efe062ab2d4c047fe7c0 to your computer and use it in GitHub Desktop.
Save kra3/f802061437e3efe062ab2d4c047fe7c0 to your computer and use it in GitHub Desktop.
flatten a nested array
// function definition ES6
let flatten = (arr, res=[]) => {
for(let itm of arr){
Array.isArray(itm)? flatten(itm, res): res.push(itm);
}
}
// calling the function
let result = [];
flatten([1,2, [3, 4], [5, [6, 7, [8, 9]]]], result);
console.log(result);
// PS: I think a better solution will be to use lodash.flatten though :p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment