Home > OS >  Remove objects that meet a condition and their children also meet the condition - Javascript
Remove objects that meet a condition and their children also meet the condition - Javascript

Time:01-31

I have this type of object:

[
{
    "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
        }
        
    ]
}

]

And I want to go through all the array of objects (children included) and delete all those objects that have the property: "__deleteConditions": true

That is to say, I want to return an array with all the objects whose "__deleteConditions" is false.

I need help, as I have not been able to solve this problem recursively. Thank you very much for your help. <3

CodePudding user response:

I think you simply need to remove all items with delete condition and then recurse on the remaining items

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)

CodePudding user response:

You can try using combination array filter and map. Please find the below.

Hope this helps

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));

  • Related