redux if already exist item dont add to array 1

redux if already exist item dont add to array

Element won't be duplicated and always at 0 index,

return {
    ...state,
    data: {
      ...state.data,
      list: [].concat(action.payload.list, state.data.list.filter(val=>val.id==action.payload.list.id))
    }
  }

Here is what the above code is Doing:
1. It’s creating a new object with the spread operator.
2. It’s creating a new data object with the spread operator.
3. It’s creating a new list array with the concat method.
4. It’s filtering the old list array and adding the new list array to the new list array.

Similar Posts