V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lizhesystem
V2EX  ›  问与答

问个 JS 递归查找 tree 的问题?

  •  
  •   lizhesystem · 2020-04-10 15:13:05 +08:00 · 1304 次点击
    这是一个创建于 1449 天前的主题,其中的信息可能已经有所发展或是发生改变。

    数据结构大致如下。

    需求是传入 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
            }
        ]
    
    
    6 条回复    2020-04-10 16:09:00 +08:00
    fancy111
        1
    fancy111  
       2020-04-10 15:16:18 +08:00
    你这个结构有问题
    lizhesystem
        2
    lizhesystem  
    OP
       2020-04-10 15:17:29 +08:00
    @fancy111 咋不对,就是普通的 tree 结构呀
    canbingzt
        3
    canbingzt  
       2020-04-10 15:46:50 +08:00   ❤️ 1
    gloye
        4
    gloye  
       2020-04-10 15:52:31 +08:00
    [4,...arr.find(item=>item.id===4).children.map(item=>item.id)]
    这样么?输入一个 id,取这个 id 和它的 children 中每个 child 的 id
    canbingzt
        5
    canbingzt  
       2020-04-10 16:05:28 +08:00
    @gloye child 的 child 也要吧
    lizhesystem
        6
    lizhesystem  
    OP
       2020-04-10 16:09:00 +08:00
    @gloye 这样得不到 child 的 child 一直到底查找
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4988 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:38 · PVG 17:38 · LAX 02:38 · JFK 05:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.