简体   繁体   English

如何删除嵌套数组/对象中的空数组?

[英]How to remove empty arrays in a nested array/object?

I have an array of objects that have array within themselves.我有一个对象数组,这些对象本身就有数组。 I want to loop through the object and delete any empty arrays that I have.我想遍历对象并删除我拥有的任何空数组。 The object is shown below:对象如下图:

let a=[{children:[{children:[1,2]},{children:[5,6]}]},
       {children:[{children:[]},{children:[5,6]}]},
       {children:[{children:[]},{children:[]}]},
       {children:[]}]

I am trying to achieve the desired result by applying the code below but I am getting an error saying cannot read property 'children' of undefined.我试图通过应用下面的代码来达到预期的结果,但我收到一条错误消息,说无法读取未定义的属性“子级”。

  function removeEmpty(array){
        for(var i=array.length-1;i>=0;i--){
            if(array[i].children){
                if(array[i].children.length){
                    for(var j=array[i].children.length-1;j=>0;j--){
                        if(array[i].children[j].children){
                            removeEmpty(array[i].children[j])
                        }else{
                            array[i].splice[j,1]
                        }
                    }
                    
                    if(!array[i].children.length){
                        array.splice(i,1)
                    }
                }else{
                    array.splice(i,1)
                }
            }
        }
    }
    
    removeEmpty(a)

Expected outcome:预期结果:

  expected outcome =[{children:[{children:[1,2]},{children:[5,6]}]},
     {children:[{children:[5,6]}]}]

If there are adjustments I can make to the existing code or use a different approach please let me know.如果我可以对现有代码进行调整或使用不同的方法,请告诉我。 Thank you.谢谢你。

To achieve your goal you can use the .reduce() method.为了实现您的目标,您可以使用.reduce()方法。

 const a = [{ children: [{ children: [1, 2] }, { children: [5, 6] }] }, { children: [{ children: [] }, { children: [5, 6] }] }, { children: [{ children: [] }, { children: [] }] }, { children: [] } ] const b = a.reduce((previousValue, currentValue) => { const data = [] if (currentValue.children.length > 0) { currentValue.children.forEach((e) => { if (e.children.length > 0) data.push(e); }); } if (data.length > 0) previousValue.push({children: data}); return previousValue; }, []); console.log(b);

Here is a prune function that reduces a node with children .这是一个 prune 函数,用于减少具有childrennode Just make sure you wrap incoming data in a node with children .只需确保将传入数据包装在带有children的节点中即可。

For each of the nodes children, you filter the children based on the child count.对于每个节点子节点,您可以根据子节点数过滤子节点。

 const data = [ { children: [{ children: [1,2] }, { children: [5,6] }] }, { children: [{ children: [] }, { children: [5,6] }] }, { children: [{ children: [] }, { children: [] }] }, { children: [] }, ]; const prune = (node, key = 'children') => node[key].reduce((prev, curr) => (children => children.length ? { [key]: [...prev[key], { [key]: children }] } : prev) (curr[key].filter(child => child[key].length)), { [key]: [] }); const tree = prune({ children: data }); tree.children.forEach(child => console.log(JSON.stringify(child)));
 .as-console-wrapper { top: 0; max-height: 100% !important; } .as-console-row-code { font-size: smaller !important; }

var updatedArray = children.filter(item => item.children.length > 0) var updatedArray = children.filter(item => item.children.length > 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM