Last active
January 15, 2018 12:09
-
-
Save miguelmota/5f27d5cdb8462fbbb402 to your computer and use it in GitHub Desktop.
Flatten array recursively in JavaScript
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 flatten(array) { | |
return Array.isArray(array) ? [].concat.apply([], array.map(flatten)) : array; | |
} | |
console.log(flatten('a')); // ["a"] | |
console.log(flatten([[['b']]])); // ["b"] | |
console.log(flatten(['a',['b'],['c']])); // ["a","b","c"] | |
console.log(flatten([['a'],'b',['c',['d']]])); // ["a","b","c","d"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment