Mateusz
Mateusz

Reputation: 1193

NGRX - updateMany with key deletion

I would like to delete particular key from object with using updateMany from Entity adapter.

Let's assume I have an array of objects :

[{id:1,key1:"test",key2:"test2"},{id:2,key1:"xxx",key2:"yyy"}]

I would like to delete key2 totally from entity.

How can I achieve it?

I tried dispatching actions with array of Update objects:

[{id: 1, changes: { [key2]: null } },{id: 2, changes: { [key2]: null } }]

then in reducer:

on(MyActions.updateCollection, (state, action) => {
    adapter.updateMany(action.updates, {
                ...state})})

But above way I am setting key2 to null, not deleting it. Any idea how to achieve it?

Upvotes: 1

Views: 486

Answers (1)

satanTime
satanTime

Reputation: 13539

you need to use upsertOne / upsertMany via passing there an updated entity without the key. To delete the key you can use a reduce function like that:

const objWithoutTheKey = Object.keys(objWithKey).reduce((result, key) => {
  if (key !== 'keyToDelete') {
    result[key] = objWithKey[key];
  }
  return result;
}, {});

Upvotes: 1

Related Questions