Created
October 20, 2017 13:50
-
-
Save mingca/52fc2da1902ee54177b9944b26b4a29d 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
/* | |
Flatten Array - exactly same with Ruby's Array#flatten | |
Example: | |
flatten([[1, 2, 3], [4, 5]]); // [1, 2, 3, 4, 5] | |
*/ | |
function flatten(arr) { | |
return arr.reduce(function (flat, toFlatten) { | |
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten); | |
}, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment