js recursively find all child nodes of the parent node json data

Description: Find all sub-organization under the framework of an organization, if this organization as well as sub-organization, to find out together.

A: json data

let orginization = 

[{
"id": "0",
"parentid": "null",
"name": "深圳市****公司"
}, {
"id": "1",
"parentid": "0",
"name": "研发部"
}, {
"id": "2",
"parentid": "0",
"name": "人力行政部"
}, {
"id": "3",
"parentid": "1",
"name": "研发一部"
}, {
"id": "4",
"parentid": "1",
"name": "研发二部"
}, {
"id": "5",
"parentid":"2", }]"name": "a small group of R & D""the parentId": "3","the above mentioned id": "7" ,}, {"name": "administrative center""the parentId": "2","the above mentioned id": "6",}, {
"name": "Human Resources Center"








 

Two: js processing

let newArray = [];

function getchild(id){
debugger
for (var i = 0; i < orginization.length; i ++) {
if (orginization[i].parentid == id) {
newArray.push(orginization[i]);
getchild(orginization[i].id);
}else{
continue;
}
}
}

Three: Print newArray call

getchild("1");

console.log(newArray);

 

Guess you like

Origin www.cnblogs.com/pipim/p/11417515.html