Hey everyone, have a problem I’m not quite sure how to solve so I figured maybe I can reach out and someone else has a good idea on what can be done here.
I have a nested object with each key being a class and the value being an object of children
and attributes
. If the key (className) in the object doesn’t have any children
or attributes
, it should be deleted. The issue is that if I check the last level in the nested object to delete the property, I’m not sure how to recursively track back up the object to delete every prop based on the children that need to be deleted.
For example:
{
".tester-class": {
"children": {
".tester-child": {
"children": {},
"attributes": {}
},
".tester-child-2": {
"children": {},
"attributes": {}
}
},
"attributes": {}
},
".another": {
"children": {},
"attributes": {}
},
"media-class": {
"children": {
".tester-class": {
"children": {
".media-child": {
"children": {
".nested-3-child": {
"children": {
".nested-4-child": {
"children": {},
"attributes": {}
}
},
"attributes": {}
}
},
"attributes": {
"color": "navy"
}
}
},
"attributes": {}
}
},
"attributes": {}
}
}
In this example above, the top level .tester-class
and .another
keys should be deleted because none of their children have attributes or children, so they should be removed, but because of the color: navy
attribute on .media-child
, that should stay. So .media-class
> .tester-class
(second one), > & .media-child
should persist, but .nested-3-child
& .nested-4-child
should be deleted because they are empty.
Theoretically, if the attribute color: navy
on .media-child
did not exist and was just an empty {}
, this entire object would be {}
because no children have attributes or children.
I first thought I could use recursion by digging all the way down until no more children exist and collect the props along the way to track back up the tree, but I am really confused here.
Does anyone have a good idea how this could be done? I’m not looking for easy answers or code, but just a brainstorming buddy to push me in the right direction on the easiest way possible for this.