js父子联动,选择某个父节点,输出父节点和下面的所有子节点
let treeData = [
{
id:1,
name:"计算机系",
children:[
{
id:101,
name:"计算机科学与技术",
children:[
{
id:10101,
name:"计一班",
children:[
{
id:1010101,
name:"小张",
},
{
id:1010102,
name:"小王",
},
{
id:1010103,
name:"小徐",
},
]
},
{
id:10102,
name:"计二班",
children:[
{
id:1010201,
name:"小武",
},
]
},
]
}
]
}
]
function getChild(tree,newArray{
for (var i = 0; i < tree.length; i ++) {
newArray.push(tree[i].id);
if(tree[i].children){
getChild(tree[i].children,newArray);
}else{
continue;
}
}
}
let arr = []
getChild(treeData[0].children,arr)
console.log(arr)