Skip to content

Instantly share code, notes, and snippets.

@kavichu
Created April 16, 2017 19:18
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 kavichu/7c8340f58e6defe277d87fbf0f1b0fd3 to your computer and use it in GitHub Desktop.
Save kavichu/7c8340f58e6defe277d87fbf0f1b0fd3 to your computer and use it in GitHub Desktop.
ES6 code to flatten array of arbitrarily nested arrays
let arr = [[1,2,[3]],4];
let flatten = (accu, e) => {
if(!Array.isArray(e)){
return [...accu, e];
}else{
return [...accu, ...e.reduce(flatten, [])];
}
}
let result = arr.reduce(flatten, [])
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment