js 递归查找

https://www.v2ex.com/t/661182

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
    }
]

function find(arr, id) {
    if (arr == null) return null;
    for (let obj of arr) {
        if (obj.id == id) {
            return collect(obj);
        }
        let ret = find(obj.children, id);
        if (ret) return ret;
    }
    return null;
}

function collect(obj) {
    let ret = [obj.id];
    if (obj.children) {
        for (let o of obj.children) {
            ret = [...ret, ...collect(o)]
        }
    }
    return ret;
}
posted @ 2020-04-10 16:01  樱风凛  阅读(4190)  评论(0编辑  收藏  举报