Last active
October 31, 2017 15:44
-
-
Save web2ls/daca0b659ecb41ba6b34ff224f2722ad to your computer and use it in GitHub Desktop.
Turn array into flat structure via Array.prototype.reduce
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(arr) { | |
| if (Array.isArray(arr)) { | |
| return arr.reduce(function(done,curr){ | |
| return done.concat(flatten(curr)); | |
| }, []); | |
| } else { | |
| return arr; | |
| } | |
| } | |
| console.log(flatten([[1,2,3], [4,5,6],[7,8,9]])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment