dot-prop-immutable icon indicating copy to clipboard operation
dot-prop-immutable copied to clipboard

Add functions to update items in array

Open cwayfinder opened this issue 7 years ago • 0 comments

Feature request

I would like to see set of methods for bulk work with arrays items. They might be built on top of existing ones (e.g. set, merge, toggle, etc).

Example of verbose code:

const units = state.units.map(unit => {
  if (unit.team === team) {
    return ({ ...unit, selected: true });
  } else {
    return unit;
  }
});
state = { ...state, units };

we can invent something like:

state = dotProps.mergeForArrayItems(state, 'units', { selected: true }, unit => unit.team === team);

or using dedicated namespace

state = dotProps.array.merge(state, 'units', { selected: true }, unit => unit.team === team);
// signature: merge(obj, prop, val [, predicate])

cwayfinder avatar Oct 28 '17 04:10 cwayfinder