Home > database >  Walk through objects nested at different levels and delete selected properties
Walk through objects nested at different levels and delete selected properties

Time:08-18

There are two types of objects. The first one is pretty simple:

{
    "status": "200",
    "dump": {
        "id": "213ad4c0",
        "product": {
            "productName": "Bicycle"
        },
        "components": {
            "steering": {
                "id": "HB2",
                "description": "Handlebar",
                "quantity": 1,
                "spare_part": false,
                "material": "steel"
            },
            "wheel": {
                "id": "WH8",
                "description": "Wheel",
                "quantity": 2,
                "spare_part": true,
                "material": "steel"
            }
        }
    }
}

I wanted to delete spare_part property from it and it could've been done with the following:

Object.entries(myResponse.dump.components).forEach(([key, value]) => {
    delete value.spare_part;
});

Things get complicated when an object is composed of nested objects like:

{
    "status": "200",
    "dump": {
        "id": "8e8cd4ee",
        "product": {
            "productName": "Car"
        },
        "components": {
            "suspension": {
                "id": "SU_02",
                "description": "Suspension",
                "quantity": 1,
                "spare_part": false,
                "material": "mixed",
                "subcomponents": {
                    "S_FRONTAL": {
                        "id": "SU_02_F",
                        "description": "Suspension Front",
                        "quantity": 1,
                        "spare_part": false,
                        "material": "mixed",
                        "subcomponents": {
                            "DAMPER_L": {
                                "id": "SU_D_L_12",
                                "description": "Damper Front Left",
                                "quantity": 1,
                                "spare_part": true,
                                "material": "mixed"
                            },
                            "DAMPER_R": {
                                "id": "SU_D_R_12",
                                "description": "Damper Front Right",
                                "quantity": 1,
                                "spare_part": true,
                                "material": "mixed"
                            }
                        }
                    }
                }
            }
        }
    }
}

How can I gracefully walk through all levels of nesting and delete the spare_part property?

By gracefully I mean no manual key chaining in Object.entries() arguments :-)

CodePudding user response:

You can use recursion, like this:

const removeSpareParts = (components) => {
  Object.values(components).forEach((component) => {
    delete component.spare_part;
    if (component.subcomponents) {
      removeSpareParts(component.subcomponents);
    }
  });
};

removeSpareParts(myResponse.dump.components);

This will go through each given layer-1 component, delete its spare part, and recursively do the same for all its subcomponents.

  • Related