Created
January 4, 2019 11:53
-
-
Save bcamarneiro/ac5498bf0d56ccb0108ade69f9f60378 to your computer and use it in GitHub Desktop.
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
//JS (es6) | |
// | |
const crazyArray = [[1,2,3], [1,2,3,4,5,[6, [7, [8]]]], [1,1,1,1,1,1,1,1]]; | |
flatten = (arr) => { | |
if(!Array.isArray(arr)){ | |
throw "Parameter is not an array!"; | |
} | |
return arr.reduce((acc, el) => { | |
if(Array.isArray(el)) { | |
return [...acc, ...flatten(el)]; | |
} | |
return [...acc, el]; | |
}, []) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment