Skip to content

Instantly share code, notes, and snippets.

@Natumsol
Created June 27, 2019 02:28
Show Gist options
  • Select an option

  • Save Natumsol/9b899b8f3f5c056364fe80003525026a to your computer and use it in GitHub Desktop.

Select an option

Save Natumsol/9b899b8f3f5c056364fe80003525026a to your computer and use it in GitHub Desktop.
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