Created
December 9, 2016 05:30
-
-
Save evanlarsen/130a1f95f213d1d03bdd85bef359d513 to your computer and use it in GitHub Desktop.
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
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