Similar to this question Remove a property in an object immutably
I have an object
parent {
child {
grandchild {},
},
},
I'm trying to flatten in a reducer
parent: {}
child: {}
grandchild: {}
With a merge function that breaks the objects up based on some requirements.
merge(state, object){
....
if(parent.child) mergeAndDelete(state, parent.child, parent, 'child')
}
function mergeAndDelete(state, type, id, ent, attrib) {
const clone = cloneDeep(ent[attrib]);
merge(state, type, id, clone);
delete ent[attrib];
}
If I delete the entity, even after the merge then the child and grandchild never make it to the reducer. They console log the whole way through. If I don't delete then they get merged as I want. But parent still has child etc.
What is the best way of going about this?