Skip to content

Instantly share code, notes, and snippets.

@kra3
Created Dec 13, 2016
Embed
What would you like to do?
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