Skip to content

Instantly share code, notes, and snippets.

@evanlarsen
Created December 9, 2016 05:30
Show Gist options
  • Save evanlarsen/130a1f95f213d1d03bdd85bef359d513 to your computer and use it in GitHub Desktop.
Save evanlarsen/130a1f95f213d1d03bdd85bef359d513 to your computer and use it in GitHub Desktop.
function flatten(arr){
if (!Array.isArray(arr)){
return [arr];
}
return arr.reduce((flat, toFlatten) => {
return flat.concat(flatten(toFlatten));
}, []);
}
function test(objUnderTest){
console.log(flatten(objUnderTest));
}
test(1);
test([1,2,3,4])
test([1,[2,3],[4,5],6,7,[[8],9]]);
test([1,[2,3],[4,5],6,7,[[8],[9,[10,[11,[12]]]]]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment