Consider I have a nested object array. One possible example scenario could be:
content: [
{
prop1: someValue,
prop2: someValue,
content: [
{
prop2: someValue,
prop3: someValue,
myProperty: myValue
},
{
prop1: someValue,
prop3: someValue,
myProperty: otherValue
}
]
},
{
prop5: someValue,
prop2: someValue
}
]
Here are the possibilities:
- The structure starts with
content[]but the descendants may or may not havecontentproperty. - The level of the hierarchy can be of any number.
- The properties contained by the objects are not always the same i.e. one object may have x, y, z properties while the other may have v, w, z properties.
- If any object in the hierarchy has
myPropertykey, there won't becontentkey. - More than one object in the hierarchy can have
myPropertywith value'myValue.
My requirement:
- If at any level an object has the property
myPropertywith the valuemyValuethen remove the entire object (NOT JUST THE PROPERTY) from the hierarchy.
My attempt so far:
private removeObjects(content: any, values: string[]): any {
if (!content || content.length === 0) {
return
}
content = content.filter((c) => {
if (!c.myProperty) return true
return c.myProperty.indexOf(values) > 0
})
// Here is my problem since I am supposed to do a recursive call on each of child contents,
// how do I merge back the original array?
return this.removeObjects(content, values)
}