Created
March 20, 2016 19:34
-
-
Save AndrejGajdos/1d56692d6da7cd8a4057 to your computer and use it in GitHub Desktop.
Function for flattening nested array
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 nested array | |
* @param {Array} array Nested array | |
* @return {Array} Flat array | |
*/ | |
function flatten(array) { | |
return array.reduce(function (flat, toFlatten) { | |
// if current item is array flatten it, otherwise concatenate value to processed flat array | |
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