I'm currently working on implementing a reducer in redux that can delete multiple items from the state based on an array of values.
Let's say I have the following array:
const idArray = ["935", "933", "930"];
My goal is to remove objects that have an id property matching any value in the idArray and create a new array with the remaining values.
const objectsArray= [
{
name: "Kevin",
color: "red",
id: "935"
},
{
name: "Ana",
color: "white",
id: "815"
},
{
name: "Maria",
color: "silver",
id: "035"
},
{
name: "Victor",
color: "red",
id: "935"
},
{
name: "Vanessa",
color: "red",
id: "933"
},
]
In this scenario, I want to eliminate the objects with names like Kevin
, Vanessa
, and Victor
, and then return a fresh array containing only Ana
and Maria
.
This is my progress in the reducer:
case PeopleActions.DELETE_PEOPLE:
if (action.id.length === 1) {
return state.filter(people =>
people.id !== action.id[0]
);
} else {
const deletePeople: any = () => {
for (let i = 0; i < action.id.length; i++) {
return state.filter(people =>
people.id !== action.id[i]
);
}
};
return deletePeople;
}