Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Walk through objects nested at different levels and delete selected properties

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "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 🙂

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading