Skip to content

Instantly share code, notes, and snippets.

@daudt
Created February 15, 2016 15:45
Show Gist options
  • Save daudt/e09e3e84017e97377970 to your computer and use it in GitHub Desktop.
Save daudt/e09e3e84017e97377970 to your computer and use it in GitHub Desktop.
converts multi dimensional array into single dimensional array
var multi = [3, 4, [5, 6, [7, 8], 9], [10, 11]];
function flatten(array) {
var simple = []
function slice(array) {
for (item of array) {
if (Array.isArray(item)) {
slice(item)
} else {
simple.push(item)
}
}
}
slice(array)
return simple
}
console.log(flatten(multi))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment