数据结构大致如下。
需求是传入 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
}
]
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.