Reactiflux
Reactiflux

help-js

Root Question Message

dmikester1
dmikester11/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?
dmikester1
dmikester11/13/2023
If it matters, this array is a React state array.
ScriptyChris
ScriptyChris1/13/2023
You can simple do a loop
ScriptyChris
ScriptyChris1/13/2023
for (const object of array) {
  object.property_to_update = 'new value';
}
ScriptyChris
ScriptyChris1/13/2023
Oh, in this case use functional update with .map() to avoid state mutation
ScriptyChris
ScriptyChris1/13/2023
setState(prev => prev.map((obj) => ({
  ...obj,
  property_to_update: 'new value'
})));
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy