Created
April 16, 2017 19:18
-
-
Save LuisValdesZero/7c8340f58e6defe277d87fbf0f1b0fd3 to your computer and use it in GitHub Desktop.
ES6 code to flatten array of arbitrarily nested arrays
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
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