Created
December 19, 2018 14:40
-
-
Save samcyn/4997504b6a41fb6d5ae201081f8d5fbc to your computer and use it in GitHub Desktop.
Flattening arrays
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
var givenArray = [1,2,[3,4,5], 6, [7,8]]; | |
function flattenController(arr) { | |
return arr.reduce(function (flat, toFlatten) { | |
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten); | |
}, []); | |
} | |
flattenController(givenArray); // [1, 2, 3, 4, 5, 6, 7, 8]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment