Skip to content

Instantly share code, notes, and snippets.

@tpps88206
Created December 7, 2020 14:36
Show Gist options
  • Save tpps88206/c5a35b0acf7348af2b0611bcf7fdd4c8 to your computer and use it in GitHub Desktop.
Save tpps88206/c5a35b0acf7348af2b0611bcf7fdd4c8 to your computer and use it in GitHub Desktop.
在 JavaScript 中實作 Flat 函數
const flatten = (input) => {
const stack = [...input];
const result = [];
while (stack.length) {
const next = stack.pop();
if (Array.isArray(next)) {
stack.push(...next);
} else {
result.push(next);
}
}
return result.reverse();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment