Skip to content

Instantly share code, notes, and snippets.

@oxyflour
Last active September 25, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oxyflour/a90ecc4616587997c950 to your computer and use it in GitHub Desktop.
Save oxyflour/a90ecc4616587997c950 to your computer and use it in GitHub Desktop.
function tree(id, arr){
var node = arr.find(a => a.id == id) || { }
node.items = arr.filter(a => a.pid == id).map(a => tree(a.id, arr))
return node
}
tree(0, [
{id:1, pid:0, name:"SYSTEM"},
{id:2, pid:1, name:"aa"},
{id:3, pid:2, name:"aaa"},
{id:4, pid:2, name:"b"},
{id:5, pid:0, name:"c"},
{id:6, pid:5, name:"cc"}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment