Skip to content

Instantly share code, notes, and snippets.

@mrprofessor
Created June 28, 2019 06:24
Show Gist options
  • Save mrprofessor/68aba70d22a1c014c4ddf6be577b3483 to your computer and use it in GitHub Desktop.
Save mrprofessor/68aba70d22a1c014c4ddf6be577b3483 to your computer and use it in GitHub Desktop.
let arr = [
{
id: 1,
parent: null,
name: "a"
},
{
id: 2,
parent: null,
name: "b"
},
{
id: 3,
parent: 1,
name: "c"
},
{
id: 4,
parent: 1,
name: "d"
},
{
id: 5,
parent: 3,
name: "e"
},
]
function get_all_children(item) {
var list = [item.id];
function node_traverse(rec) {
for(item of arr) {
if(rec.id === item.parent) {
list.push(item.id)
node_traverse(item)
}
}
}
node_traverse(item)
return list;
}
for(item of arr) {
const children_list = get_all_children(item);
console.log(children_list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment