Last active
April 18, 2018 11:10
-
-
Save dfernandeza/50319675fbef639439348946e4bb08f5 to your computer and use it in GitHub Desktop.
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers
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
| let arr = [[1, 2, [3]], 4]; | |
| let flatten = a => a.reduce((acc, v) => Array.isArray(v) ? [...acc, ...flatten(v)] : [...acc, v], []); | |
| console.log(flatten(arr)); // [1, 2, 3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment