Last active
May 30, 2016 09:43
-
-
Save jeankueo/58a8a9152cc1a5cc083565220eafc8e9 to your computer and use it in GitHub Desktop.
flatten an unknown depth array
This file contains hidden or 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 flatArray (source) { | |
if (Array.isArray(source)) { | |
return source.reduce(function (previous, current) { | |
return previous.concat(flatArray(current)); | |
}, []) | |
} | |
return [source]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This practice helps developers to understand array methods. Especially some new ones in ES5.