数据结构大致如下。
需求是传入 id 递归查出来当前节点下面所有的 id,比如传 function(arr,1){}得到所有的 id,因为 1 是根,传 4 得到[4,26]
感觉好难啊,自己只能写出来递归找到某个 id 。
let arr = [
{
"children": [
{
"pid": 1,
"id": 2,
"label": "技术",
"value": 2
},
{
"pid": 1,
"id": 3,
"label": "产品",
"value": 3
},
{
"children": [
{
"pid": 4,
"id": 26,
"label": "测试栏目 1",
"value": 26
}
],
"pid": 1,
"id": 4,
"label": "项目",
"value": 4
},
{
"pid": 1,
"id": 5,
"label": "国际",
"value": 5
},
{
"pid": 1,
"id": 24,
"label": "研究",
"value": 24
},
{
"children": [
{
"pid": 7,
"id": 18,
"label": "行业",
"value": 18
},
{
"pid": 7,
"id": 19,
"label": "活动",
"value": 19
},
{
"pid": 7,
"id": 20,
"label": "新闻",
"value": 20
},
{
"pid": 7,
"id": 21,
"label": "资讯",
"value": 21
},
{
"pid": 7,
"id": 22,
"label": "新闻",
"value": 22
},
{
"pid": 7,
"id": 23,
"label": "知识",
"value": 23
},
{
"children": [
{
"children": [
{
"pid": 11,
"id": 12,
"label": "栏目 4",
"value": 12
}
],
"pid": 9,
"id": 11,
"label": "栏目 3",
"value": 11
}
],
"pid": 7,
"id": 9,
"label": "帮助中心",
"value": 9
}
],
"pid": 1,
"id": 7,
"label": "新闻资讯",
"value": 7
},
{
"children": [
{
"pid": 8,
"id": 13,
"label": "简介",
"value": 13
},
{
"pid": 8,
"id": 14,
"label": "荣誉",
"value": 14
},
{
"pid": 8,
"id": 15,
"label": "关怀",
"value": 15
},
{
"pid": 8,
"id": 16,
"label": "我们",
"value": 16
}
],
"pid": 1,
"id": 8,
"label": "我们",
"value": 8
},
{
"pid": 1,
"id": 17,
"label": "公告",
"value": 17
},
{
"pid": 1,
"id": 6,
"label": "展示",
"value": 6
}
],
"pid": 0,
"id": 1,
"label": "主目录",
"value": 1
}
]
1
fancy111 2020-04-10 15:16:18 +08:00
你这个结构有问题
|
2
lizhesystem OP @fancy111 咋不对,就是普通的 tree 结构呀
|
3
canbingzt 2020-04-10 15:46:50 +08:00 1
|
4
gloye 2020-04-10 15:52:31 +08:00
|
6
lizhesystem OP @gloye 这样得不到 child 的 child 一直到底查找
|