Created
February 15, 2016 15:45
-
-
Save daudt/e09e3e84017e97377970 to your computer and use it in GitHub Desktop.
converts multi dimensional array into single dimensional 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
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