Skip to content

Instantly share code, notes, and snippets.

@saeta-eth
Created December 28, 2017 21:30
Show Gist options
  • Save saeta-eth/3369a1408f0611b9ad6ebff61407d9ef to your computer and use it in GitHub Desktop.
Save saeta-eth/3369a1408f0611b9ad6ebff61407d9ef to your computer and use it in GitHub Desktop.
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