Created
June 12, 2016 15:50
-
-
Save cky/db218f63441e292fabf50d03c0f1b722 to your computer and use it in GitHub Desktop.
One way to flatten in ES6, not necessarily the best
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
function flatten(x) { | |
var result = []; | |
var appendToResult = function (xs) { | |
result.splice(result.length, 0, ...xs); | |
} | |
if (Array.isArray(x)) { | |
x.map(flatten).forEach(appendToResult); | |
} else { | |
result.push(x); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment