Created
December 19, 2017 07:40
-
-
Save black-black-cat/ed5799c80a745b85d9d9b4acfeaec52f to your computer and use it in GitHub Desktop.
flatten 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 flatten(arr, result) { | |
| result = result || [] | |
| arr.forEach(function(item) { | |
| if (Array.isArray(item)) { | |
| flatten(item, result) | |
| } else { | |
| result.push(item) | |
| } | |
| }) | |
| return result | |
| } | |
| // test | |
| console.log(flatten([ | |
| [3], | |
| [[5], [[[9]]]] | |
| ])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment