Skip to content

Instantly share code, notes, and snippets.

@R1ZEN
Created April 2, 2019 09:19
Show Gist options
  • Save R1ZEN/7793ce7bba7058263dc57a939095fa42 to your computer and use it in GitHub Desktop.
Save R1ZEN/7793ce7bba7058263dc57a939095fa42 to your computer and use it in GitHub Desktop.
Flattern
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