Shanah Suping
Shanah Suping

Reputation: 39

NGRX - How to use filter to delete an item from an array?

I am trying to delete an item from an array but when I execute the code, it removes all the items from the state instead of the ones that dont have the specified ID.

    case REMOVE_STORE:
      return {
        Stores:[...state.Stores.filter( (item) => {
          item.storeId != action.payload
        })],
      };

I am able to remove an item by making use of slice, and the position of the element in the array but I would like to make use of the ID instead of the position in the array.

Upvotes: 0

Views: 551

Answers (1)

ViqMontana
ViqMontana

Reputation: 5688

You're missing the return statement. Change you code to:

case REMOVE_STORE:
return {
    Stores: [...state.Stores.filter((item) => {
        return item.storeId != action.payload
    })],
};

Upvotes: 2

Related Questions