Last active
May 29, 2017 22:19
-
-
Save manatax/2c750a33a7f8540f7a1646503855be6e to your computer and use it in GitHub Desktop.
flatten an array of arbitrarily nested arrays
This file contains 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 data = [[1,2,[3]],4]; | |
function flatten(array) { | |
let res = []; | |
for (let item of array) { | |
if (Array.isArray(item)) { | |
res = res.concat(flatten(item)); | |
} else { | |
res.push(item); | |
} | |
} | |
return res; | |
} | |
console.log(flatten(data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment