How to remove certain property from all objects inside a object
Advanced
Sometimes you have a data object, with a set of objects inside it and maybe even object inside each child object and you want to get certain property removed from all these child objects. Here’s one way to do it.
// This ID identifies property2 child we want to keep while removing all others // This could be for example userId let objId = "GS46456"; // This is our original data let originalObject = { 5345jiji4: { property1: "text", property2: { GS46456: "I want to keep this one", XX45558: "I want to remove this one", YHOP622: "I want to remove this one", } }, 3345jixx8: { property1: "another text", property2: { GS46456: "I want to keep this one", XX45558: "I want to remove this one", YHOP622: "I want to remove this one", } }, } let filteredObject = {}; // This where we want end result Object.keys(originalObject).map((key) => { let obj = originalObject[key] // if (obj.property2) { Object.keys(obj.property2).map((key) => { if (key !== objId) { delete obj.property2[key]; } }); } filteredObject[key] = obj; // Returns modified object to our target object }); return filteredObject;