Created
June 28, 2019 06:24
-
-
Save mrprofessor/68aba70d22a1c014c4ddf6be577b3483 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
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