根据条件从嵌套对象数组中删除项目

Codegeek

在我的应用程序中,我从服务器返回了如下数据。它具有非常深的嵌套:

var data = [{
    name: "root",
    children: [{
            name: "Parent1",
            children: [{
                    name: "Parent1-child1",
                    children: [{
                            name: "Parent1-child1-grandchild1",
                            children: [{
                                name: "Parent1-child1-grandchild1-last",
                                children:[]
                            }]
                        },
                        {
                            name: "Parent1-child1-grandchild2",
                            children: []
                        },
                        {
                            name: "Parent1-child1-grandchild3",
                            children: []
                        }
                    ]
                },
                {
                    name: "Paren1-child2",
                    children: [{
                            name: "Parent1-chil2-grandchild1",
                            children: []
                        },
                        {
                            name: "Parent1-child2-grandchild2",
                            children: [{
                                name: "Parent1-child2-grandchild2-last",
                                children: []
                            }]
                        },
                        {
                            name: "Parent1-child2-grandchild3",
                            children: []
                        }
                    ]
                },
                {
                    name: "Parent1-child3",
                    children: []
                }
            ]
        },
        {
            name: "Parent2",
            children: [{
                    name: "Parent2-child1",
                    children: []
                },
                {
                    name: "Parent2-child2",
                    children: [{
                            name: "Parent2-child2-grandchild1",
                            children: []
                        },
                        {
                            name: "Parent2-child2-grandchild2",
                            children: [{
                                name: "Parent2-child2-grandchild2-last",
                                children: []
                            }]
                        }
                    ]
                }
            ]
        },
        {
            name: "Parent3",
            children: []
        }
    ]
}];

要求是遍历所有对象(也为深层嵌套层),如果children属性的值为空数组,则删除该对象。所以输出应该像下面这样

var data = [{
    name: "root",
    children: [{
            name: "Parent1",
            children: [{
                    name: "Parent1-child1",
                    children: [{
                            name: "Parent1-child1-grandchild1",
                            children: []
                        },
                    ]
                },
                {
                    name: "Paren1-child2",
                    children: [
                        {
                            name: "Parent1-child2-grandchild2",
                            children: []
                        },
                    ]
                },
            ]
        },
        {
            name: "Parent2",
            children: [
                {
                    name: "Parent2-child2",
                    children: [
                        {
                            name: "Parent2-child2-grandchild2",
                            children: []
                        }
                    ]
                }
            ]
        }
    ]
}];

我已经尝试了以下代码,但是无法正常工作。请让我知道如何达到预期的结果。

function checkChildrens(arr) {
    arr.forEach((ele,i) => {
    if(ele.hasOwnProperty('children')) {
        checkChildrens(ele['children']) 
    } else {
        arr.splice(i,1)
    }
    })
}
checkChildrens(data);

在这种情况下,我也尝试过使用filter方法。它无法正常工作。

arr.filter((ele,i)=>{
   if(ele.hasOwnProperty('children') && ele.children.length !== 0 ){
      removeEmpty(ele.children)
   }else{
        return false;
    }
    return true;
})
妮娜·斯科茨(Nina Scholz)

您可以通过检查子数组的长度来重建新对象。

function filter(array) {
    return array.reduce((r, o) => {
        if (o.children && o.children.length) {
            r.push(Object.assign({}, o, { children: filter(o.children) }));
        }
        return r;
    }, []);
}

var data = [{ name: "root", children: [{ name: "Parent1", children: [{ name: "Parent1-child1", children: [{ name: "Parent1-child1-grandchild1", children: [{ name: "Parent1-child1-grandchild1-last", children: [] }] }, { name: "Parent1-child1-grandchild2", children: [] }, { name: "Parent1-child1-grandchild3", children: [] }] }, { name: "Paren1-child2", children: [{ name: "Parent1-chil2-grandchild1", children: [] }, { name: "Parent1-child2-grandchild2", children: [{ name: "Parent1-child2-grandchild2-last", children: [] }] }, { name: "Parent1-child2-grandchild3", children: [] }] }, { name: "Parent1-child3", children: [] }] }, { name: "Parent2", children: [{ name: "Parent2-child1", children: [] }, { name: "Parent2-child2", children: [{ name: "Parent2-child2-grandchild1", children: [] }, { name: "Parent2-child2-grandchild2", children: [{ name: "Parent2-child2-grandchild2-last", children: [] }] }] }] }, { name: "Parent3", children: [] }] }],
    result = filter(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

删除所有嵌套的空子级的方法(最后一个子级除外。该子级有一个空对象,但没有子级属性)。

function filter(array) {
    return array.reduce((r, o) => {
        if (o.children) {
            var children = filter(o.children);
            if (children.length) r.push(Object.assign({}, o, { children }));
        } else {
            r.push(o);
        }
        return r;
    }, []);
}

var data = [{ name: "root", children: [{ name: "Parent1", children: [{ name: "Parent1-child1", children: [{ name: "Parent1-child1-grandchild1", children: [{ name: "Parent1-child1-grandchild1-last", children: [] }] }, { name: "Parent1-child1-grandchild2", children: [] }, { name: "Parent1-child1-grandchild3", children: [] }] }, { name: "Paren1-child2", children: [{ name: "Parent1-chil2-grandchild1", children: [] }, { name: "Parent1-child2-grandchild2", children: [{ name: "Parent1-child2-grandchild2-last", children: [] }] }, { name: "Parent1-child2-grandchild3", children: [] }] }, { name: "Parent1-child3", children: [] }] }, { name: "Parent2", children: [{ name: "Parent2-child1", children: [] }, { name: "Parent2-child2", children: [{ name: "Parent2-child2-grandchild1", children: [] }, { name: "Parent2-child2-grandchild2", children: [{ name: "Parent2-child2-grandchild2-last", children: [] }] }] }] }, { name: "Parent3", children: [{}] }] }],
    result = filter(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

根据条件从数组中删除对象数组

来自分类Dev

嵌套JSON,使用angularjs根据Jquery中的条件删除对象

来自分类常见问题

如何根据条件向数组中的嵌套对象添加属性

来自分类Dev

根据嵌套数组中的条件选择对象

来自分类Dev

如何根据条件向数组中的嵌套对象添加属性

来自分类Dev

删除JS嵌套对象数组中任何级别的项目

来自分类Dev

如何根据多个条件从数组中删除对象?

来自分类Dev

根据条件从数组中删除特定项目,并在javascript中返回其他项目

来自分类Dev

如何根据给定条件从对象的嵌套数组中获取数组中的值?

来自分类Dev

如何在 iOS Swift 中的嵌套数组(对多关系)中根据条件过滤对象数组

来自分类Dev

如何使用最新的MongoDB C#驱动程序根据多个条件删除复杂数组对象的嵌套数组中的元素?

来自分类Dev

根据条件删除嵌套JSON中的元素

来自分类Dev

从数组中删除对象或嵌套对象

来自分类Dev

根据对象值从数组中删除对象

来自分类Dev

如何根据多个对象属性有条件地删除数组中的对象?

来自分类Dev

根据条件从数组中多次删除元素

来自分类Dev

根据两个条件从JavaScript中的对象数组中删除对象(重复的mac地址&使用的条件小于或大于)

来自分类Dev

从对象中删除嵌套数组

来自分类Dev

如何从嵌套对象数组中删除元素?

来自分类Dev

根据另一个对象数组过滤对象数组。删除数组值与另一个对象数组中的数组值匹配的项目

来自分类Dev

ReactJS从状态(对象数组)中删除项目

来自分类Dev

如何从对象数组中删除项目?

来自分类Dev

根据嵌套数组中的值删除数组元素

来自分类Dev

从具有嵌套项目的数组中删除项目

来自分类Dev

从嵌套在对象中的数组中删除对象

来自分类Dev

获取与嵌套对象中的条件匹配的对象数组

来自分类Dev

使用 Typescript 中的条件对象过滤嵌套数组的数组

来自分类Dev

如何从 PHP 中的嵌套数组中删除项目?

来自分类Dev

Javascript-从嵌套对象数组中删除对象

Related 相关文章

  1. 1

    根据条件从数组中删除对象数组

  2. 2

    嵌套JSON,使用angularjs根据Jquery中的条件删除对象

  3. 3

    如何根据条件向数组中的嵌套对象添加属性

  4. 4

    根据嵌套数组中的条件选择对象

  5. 5

    如何根据条件向数组中的嵌套对象添加属性

  6. 6

    删除JS嵌套对象数组中任何级别的项目

  7. 7

    如何根据多个条件从数组中删除对象?

  8. 8

    根据条件从数组中删除特定项目,并在javascript中返回其他项目

  9. 9

    如何根据给定条件从对象的嵌套数组中获取数组中的值?

  10. 10

    如何在 iOS Swift 中的嵌套数组(对多关系)中根据条件过滤对象数组

  11. 11

    如何使用最新的MongoDB C#驱动程序根据多个条件删除复杂数组对象的嵌套数组中的元素?

  12. 12

    根据条件删除嵌套JSON中的元素

  13. 13

    从数组中删除对象或嵌套对象

  14. 14

    根据对象值从数组中删除对象

  15. 15

    如何根据多个对象属性有条件地删除数组中的对象?

  16. 16

    根据条件从数组中多次删除元素

  17. 17

    根据两个条件从JavaScript中的对象数组中删除对象(重复的mac地址&使用的条件小于或大于)

  18. 18

    从对象中删除嵌套数组

  19. 19

    如何从嵌套对象数组中删除元素?

  20. 20

    根据另一个对象数组过滤对象数组。删除数组值与另一个对象数组中的数组值匹配的项目

  21. 21

    ReactJS从状态(对象数组)中删除项目

  22. 22

    如何从对象数组中删除项目?

  23. 23

    根据嵌套数组中的值删除数组元素

  24. 24

    从具有嵌套项目的数组中删除项目

  25. 25

    从嵌套在对象中的数组中删除对象

  26. 26

    获取与嵌套对象中的条件匹配的对象数组

  27. 27

    使用 Typescript 中的条件对象过滤嵌套数组的数组

  28. 28

    如何从 PHP 中的嵌套数组中删除项目?

  29. 29

    Javascript-从嵌套对象数组中删除对象

热门标签

归档