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 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。