R
Reactiflux

help-js

dmikester1 – 22-28 Jan 13

Ddmikester11/13/2023
I have an array of objects. Each object has many fields, but I want to update the linePosition field in all of the objects. I simply want to decrement it by one. Would map be my best bet here?
If it matters, this array is a React state array.
Sscriptychris1/13/2023
You can simple do a loop
for (const object of array) {
  object.property_to_update = 'new value';
}

Oh, in this case use functional update with .map() to avoid state mutation
setState(prev => prev.map((obj) => ({
  ...obj,
  property_to_update: 'new value'
})));
UUUnknown User1/14/2023
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!