Created
December 13, 2016 19:10
-
-
Save kra3/f802061437e3efe062ab2d4c047fe7c0 to your computer and use it in GitHub Desktop.
flatten a nested array
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
// 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