Skip to content

Instantly share code, notes, and snippets.

@bcamarneiro
Created January 4, 2019 11:53
Show Gist options
  • Save bcamarneiro/ac5498bf0d56ccb0108ade69f9f60378 to your computer and use it in GitHub Desktop.
Save bcamarneiro/ac5498bf0d56ccb0108ade69f9f60378 to your computer and use it in GitHub Desktop.
//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