Created
June 27, 2019 02:28
-
-
Save Natumsol/9b899b8f3f5c056364fe80003525026a to your computer and use it in GitHub Desktop.
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
| var list = [1, 1, 2, 3,3,3, 2, 1, 2, 1]; | |
| function parseList(result, node) { | |
| if (result.length) { | |
| const parent = result[result.length - 1]; | |
| if (parent.value < node) { | |
| if (!parent.children) | |
| parent.children = []; | |
| parent.children = parseList(parent.children, node) | |
| } else { | |
| result.push({ | |
| value: node | |
| }) | |
| } | |
| } else { | |
| result.push({value:node}) | |
| } | |
| return result; | |
| } | |
| let result = []; | |
| list.forEach(item=>{ | |
| result = parseList(result, item) | |
| } | |
| ) | |
| console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment