js多层数组对象遍历操作

寅春树
  • 679
[
        {
           
            "id":"111",          
            "departName":"助安科技",           
            "children":[
                {
               
                    "id":"222",                   
                    "departName":"生产管理部",                  
                    "children":null
                },
                {                  
                    "id":"333",                  
                    "departName":"设备管理部",                  
                    "children":[
                        {                          
                            "id":"444",                          
                            "departName":"项目1组",                          
                            "children":null
                        },
                        {                        
                            "id":"555",                           
                            "departName":"软件研发部",                           
                            "children":null
                        }
                    ]
                }
            ]
        }
...
    ],

在数数据和层级不确定的情况下,我怎么遍历所有层级去判断他们的childer是否为null?

回复
阅读 503
3 个回答
✓ 已被采纳
function every(list) {
    return list.every(v => v.children != null && every(v.children))
}

这种用递归最适合了

宣传栏