<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
/*
id: 子节点的pid,
pid: 0代表顶级数据 父id相当于子数据的pid
label: 信息
children: 数组 子节点数据
*/
data = [{
id: 1,
label: '一中队',
children: [{
id: 4,
label: '一支队',
children: [
{
id: 9,
label: '张三'
}, {
id: 10,
label: '李四',
children:[
{
id: 110,
label: '李110四',
}
]
}
]
}]
}, {
id: 6,
label: '四中队',
}]
var returnedItem = []; //定义一个空数组
function find(arr, id) {
//利用foreach循环遍历
arr.forEach((item) => {
//判断递归结束条件
if (item.id == id) {
// 存储数据到空数组
returnedItem=item.children;
} else if (item.children != null) //判断chlidren是否有数据
{
//递归调用
find(item.children, id);
}
})
return returnedItem;
}
var item = new find(data, 10);
console.log(item)
</script>
</body>
</html>