Created
April 2, 2019 09:19
-
-
Save R1ZEN/7793ce7bba7058263dc57a939095fa42 to your computer and use it in GitHub Desktop.
Flattern
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
function flattern(arr) { | |
const result = []; | |
const stack = [arr]; | |
let temp; | |
let length; | |
let value; | |
let i; | |
while(stack.length !== 0){ | |
temp = stack.pop(); | |
length = temp.length; | |
for (i = 0; i < length; i++) { | |
value = temp[i]; | |
if (Array.isArray(value)) { | |
stack.unshift(value); | |
stack.unshift(temp.slice(i + 1)); | |
break; | |
} | |
result.push(value); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment