移除满足条件的对象及其子对象- Javascript

p4rjhz4m  于 2023-02-02  发布在  Java
关注(0)|答案(3)|浏览(129)

我有这种类型的对象:

[
{
    "fieldID": 10,
    "isGroup": true,
    "itemDisplayName": "Cliente",
    "items": [
        {
            "fieldID": 1,
            "isGroup": false,
            "itemDisplayName": "Código cliente",
            "items": [],
            "__deleteConditions": false
        },
        {
            "fieldID": 2,
            "isGroup": false,
            "itemDisplayName": "Apellidos",
            "items": [],
            "__deleteConditions": true
        }
    ]
}, 
{
    "fieldID": 20,
    "isGroup": true,
    "itemDisplayName": "Aseguradora",
    "items": [
        {
            "fieldID": 3,
            "isGroup": false,
            "itemDisplayName": "Código",
            "items": [
                {
                    "fieldID": 4,
                    "isGroup": false,
                    "itemDisplayName": "Id",
                    "items": [
                        {
                            "fieldID": 5,
                            "isGroup": false,
                            "itemDisplayName": "Numerical",
                            "items": [],
                            "__deleteConditions": true
                        },
                        {
                            "fieldID": 7,
                            "isGroup": false,
                            "itemDisplayName": "Numerical2",
                            "items": [],
                            "__deleteConditions": false
                        }
                    ],
                    "__deleteConditions": false
                },
                {
                    "fieldID": 6,
                    "isGroup": false,
                    "itemDisplayName": "Apellidos",
                    "items": [],
                    "__deleteConditions": true
                }
            ],
            "__deleteConditions": false
        }
        
    ]
}

]
我想要遍历所有的对象数组(包括子对象)并删除所有具有属性的对象:“__删除条件”:真
也就是说,我希望返回一个数组,其中包含“__deleteConditions”为false的所有对象。
我需要帮助,因为我一直无法递归地解决这个问题。非常感谢您的帮助。〈3

agxfikkp

agxfikkp1#

您可以尝试使用组合数组过滤器和Map。请查看以下内容。
希望这能帮上忙

const data = [
{
    "fieldID": 10,
    "isGroup": true,
    "itemDisplayName": "Cliente",
    "items": [
        {
            "fieldID": 1,
            "isGroup": false,
            "itemDisplayName": "Código cliente",
            "items": [],
            "__deleteConditions": false
        },
        {
            "fieldID": 2,
            "isGroup": false,
            "itemDisplayName": "Apellidos",
            "items": [],
            "__deleteConditions": true
        }
    ]
}, 
{
    "fieldID": 20,
    "isGroup": true,
    "itemDisplayName": "Aseguradora",
    "items": [
        {
            "fieldID": 3,
            "isGroup": false,
            "itemDisplayName": "Código",
            "items": [
                {
                    "fieldID": 4,
                    "isGroup": false,
                    "itemDisplayName": "Id",
                    "items": [
                        {
                            "fieldID": 5,
                            "isGroup": false,
                            "itemDisplayName": "Numerical",
                            "items": [],
                            "__deleteConditions": true
                        },
                        {
                            "fieldID": 7,
                            "isGroup": false,
                            "itemDisplayName": "Numerical2",
                            "items": [],
                            "__deleteConditions": false
                        }
                    ],
                    "__deleteConditions": false
                },
                {
                    "fieldID": 6,
                    "isGroup": false,
                    "itemDisplayName": "Apellidos",
                    "items": [],
                    "__deleteConditions": true
                }
            ],
            "__deleteConditions": false
        }
        
    ]
}]

  
  const arrayToTree = (arr) =>
  arr.filter(item => !item.__deleteConditions)
    .map(child => ({ ...child, items: child.items?.length ? arrayToTree(child.items) : []  }));
  
 

console.log('result', arrayToTree(data));
0x6upsns

0x6upsns2#

将其构建在一个泛型函数之上似乎很有用,该函数将根据任意 predicate 过滤您的item-children对象,然后通过向其传递一个检查__deleteCondition的函数来编写您的版本。
我们可以这样做:

const deepFilter = (fn) => (xs) => 
  xs .filter (fn) 
     .map (({items = [], ...rest}) => ({...rest, items: deepFilter (fn) (items)}))

const removeDeleteCondition = deepFilter ((x) => ! x .__deleteConditions) 

const input = [{fieldID: 10, isGroup: true, itemDisplayName: "Cliente", items: [{fieldID: 1, isGroup: false, itemDisplayName: "Código cliente", items: [], __deleteConditions: false}, {fieldID: 2, isGroup: false, itemDisplayName: "Apellidos", items: [], __deleteConditions: true}]}, {fieldID: 20, isGroup: true, itemDisplayName: "Aseguradora", items: [{fieldID: 3, isGroup: false, itemDisplayName: "Código", items: [{fieldID: 4, isGroup: false, itemDisplayName: "Id", items: [{fieldID: 5, isGroup: false, itemDisplayName: "Numerical", items: [], __deleteConditions: true}, {fieldID: 7, isGroup: false, itemDisplayName: "Numerical2", items: [], __deleteConditions: false}], __deleteConditions: false}, {fieldID: 6, isGroup: false, itemDisplayName: "Apellidos", items: [], __deleteConditions: true}], __deleteConditions: false}]}]

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

deepFilter是我们的通用、可重用函数,能够在我们系统的其他部分或我们可能创建的其他系统上使用。removeDeleteCondition是通过向deepFilter传递 predicate 函数的简单技术创建的。

ldxq2e6h

ldxq2e6h3#

我认为您只需要使用delete条件删除所有项,然后递归处理剩余项

const data = [
{
    "fieldID": 10,
    "isGroup": true,
    "itemDisplayName": "Cliente",
    "items": [
        {
            "fieldID": 1,
            "isGroup": false,
            "itemDisplayName": "Código cliente",
            "items": [],
            "__deleteConditions": false
        },
        {
            "fieldID": 2,
            "isGroup": false,
            "itemDisplayName": "Apellidos",
            "items": [],
            "__deleteConditions": true
        }
    ]
}, 
{
    "fieldID": 20,
    "isGroup": true,
    "itemDisplayName": "Aseguradora",
    "items": [
        {
            "fieldID": 3,
            "isGroup": false,
            "itemDisplayName": "Código",
            "items": [
                {
                    "fieldID": 4,
                    "isGroup": false,
                    "itemDisplayName": "Id",
                    "items": [
                        {
                            "fieldID": 5,
                            "isGroup": false,
                            "itemDisplayName": "Numerical",
                            "items": [],
                            "__deleteConditions": true
                        },
                        {
                            "fieldID": 7,
                            "isGroup": false,
                            "itemDisplayName": "Numerical2",
                            "items": [],
                            "__deleteConditions": false
                        }
                    ],
                    "__deleteConditions": false
                },
                {
                    "fieldID": 6,
                    "isGroup": false,
                    "itemDisplayName": "Apellidos",
                    "items": [],
                    "__deleteConditions": true
                }
            ],
            "__deleteConditions": false
        }
        
    ]
}]

const clearEntry = (entry) => {
  entry.items = entry.items.filter(item => !item.__deleteConditions)
  entry.items.forEach(clearEntry)
}

data.forEach(clearEntry)
console.log(data)

相关问题