Created
December 7, 2020 14:36
-
-
Save tpps88206/c5a35b0acf7348af2b0611bcf7fdd4c8 to your computer and use it in GitHub Desktop.
在 JavaScript 中實作 Flat 函數
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
| 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