I am working on an app with a normalized state in Redux. One of my entities is the 'parent' entity of another, so when I delete that parent entity I want to delete all of the children entities associated with that parent.
For deleting a single entity (1 ID), I have been using this pattern:
// ../reducers/entity.js
case DELETE_ENTITY: {
const entityId = action.payload.entityId;
const {[entityId]: ignore, ...stateWithoutEntity} = state;
return stateWithoutEntity;
}
For context, the state
in the above snippet is shaped as such:
{
ID_1: {
//entity 1 value
},
ID_2: {
//entity 2 value
},
// etc...
}
Is there a similar patten for deleting a list of multiple entities (n IDs)?
In other words, is there a pattern for cloning a JavaScript object while excluding several keys?
// ../reducers/entity.js
case DELETE_PARENT_ENTITY: {
const parentId = action.payload.parentId;
const childrenIdsToRemove = action.payload.children;
// How do I clone the state while excluding each ID in childrenIdsToRemove?
}