Created
December 28, 2017 21:30
-
-
Save saeta-eth/3369a1408f0611b9ad6ebff61407d9ef to your computer and use it in GitHub Desktop.
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
const deepFlatten = function(arr) { | |
const flatten = function(arr) { | |
return [].concat(...arr) | |
} | |
return flatten( // return shalowly flattened array | |
arr.map(x=> // with each x in array | |
Array.isArray(x) // is x an array? | |
? deepFlatten(x) // if yes, return deeply flattened x | |
: x // if no, return just x | |
) | |
) | |
} | |
const dimensionalArray = [ | |
[ | |
[ | |
[1, 2], | |
[3, 4] | |
], | |
[ | |
[5, 6], | |
[7, 8] | |
] | |
], | |
[ | |
[ | |
[9, 10], | |
[11, 12] | |
], | |
[ | |
[13, 14], | |
[15, 16, [17]], | |
[18, 19] | |
] | |
], | |
[ | |
20 | |
] | |
]; | |
console.log(deepFlatten(dimensionalArray)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment