Created
November 1, 2018 20:16
-
-
Save michelarteta/87130e389095730f9b78075e1da07917 to your computer and use it in GitHub Desktop.
Flattering 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
// case [[1,2,[3]],4] | |
// case [[1, 2],[3, 4, 5], [6, 7, 8, 9]]; | |
var array = [[1, 2],[3, 4, 5], [6, 7, 8, 9]]; | |
function flattenArray(value) { | |
const stack = [...value]; | |
const res = []; | |
while (stack.length) { | |
const next = stack.pop(); | |
if (Array.isArray(next)) { | |
stack.push(...next); | |
} else { | |
res.push(next); | |
} | |
} | |
return res.reverse(); | |
} | |
flattenArray(array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment