Home > Net >  How to update all entities at once - NGRX?
How to update all entities at once - NGRX?

Time:10-30

I need to update the isExpanded property in all entities. I tried to do this with reduce() but got nested objects with v key :/

function updateAllIsExpanded(state, isExpanded): any {
  return Object.entries(state.entities).reduce(
    (p, [k, v]) => ({ ...p, [k]: { v, ...{ isExpanded } } }),
    {}
  );
}

In ngrx documentation we can find something like updateMany... but the problem is that I have to create arrays of objects with id and change... so I guess that's not a good idea ...

CodePudding user response:

You need ... in front of v:

UPDATE:

function updateAllIsExpanded(state, isExpanded): any {
  return Object.entries(state.entities).reduce(
    (p, [k, v]: [string, Object]) => ({ ...p, [k]: { ...v, ...{ isExpanded } } }),
    {}
  );
}
  • Related