Search code examples
angularngrx

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.


Solution

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

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