new^ 2021-10-18 09:50 采纳率: 53.3%
浏览 85
已结题

求一个js数组深层遍历的算法急!


[
    {
        "ent": "node",
        "Id": "e026ee44b8ebae9eead1c496591a41a7",
        "type": true,
        "children": [
            {
                "ent": "node",
                "Id": "e026ee44b8ebae9eead1c496591a41a7",
                "type": false,
                "children": [
                    {
                        "aaa": "1",
                        "bbb": "2",
                        "ent": "node1",
                        "Id": "e026ee44b8ebae9eead1c1123",
                        "children": [
                            {
                                "ent": "922220",
                                "children": []
                            }
                        ]
                    },
                    {
                        "aaa": "3",
                        "bbb": "4",
                        "ent": "node2",
                        "Id": "4312e026ee44b8ebae9eead1c1123",
                        "type": false,
                        "children": [
                            {
                                "ent": "9666",
                                "children": []
                            }
                        ]
                    },
                    {
                        "aaa": "3",
                        "bbb": "4",
                        "ent": "node2",
                        "Id": "4312e026ee44b8ebae9eead1c1123",
                        "children": [
                            {
                                "aaa": "3",
                                "bbb": "4",
                                "ent": "node2",
                                "Id": "4312e026ee44b8ebae9eead1c1123",
                                "children": [
                                    {
                                        "aaa": "8",
                                        "bbb": "9",
                                        "ent": "3",
                                        "Id": "4312e026ee44b8ebae9eead1c11212321111111111113",
                                        "type": true,
                                        "children": [
                                            {
                                                "ent": "90",
                                                "children": []
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

数组不确定有多少级,需要遍历每个children,查找到在最深层type为true的一级,返回这个json对象的算法!

  • 写回答

3条回答 默认 最新

  • 小朋友120 2021-10-18 10:20
    关注
    
     const data = 你的数据
    
    const getTreeName = (list, ids) => {
        for (let i = 0; i < list.length; i++) {
            //  找到满足条件的  返回整个 list[i]
            if (list[i].type === ids) return list[i];
            // 循环遍历判断 有没有下一级 children
            if (list[i]?.children && list[i]?.children.length > 0) {
                // 如果有,则传入 children 和指定ids 然后递归调用 getTreeName()
                const res = getTreeName(list[i]?.children, ids);
                if (res) return res;
            }
        }
    };
    
    console.log(getTreeName(data,true))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月26日
  • 已采纳回答 10月18日
  • 修改了问题 10月18日
  • 修改了问题 10月18日
  • 展开全部

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题