dmikester1
dmikester12y ago

dmikester1 – 22-28 Jan 13

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?
3 Replies
dmikester1
dmikester12y ago
If it matters, this array is a React state array.
ScriptyChris
ScriptyChris2y ago
You can simple do a loop
for (const object of array) {
object.property_to_update = 'new value';
}
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'
})));
setState(prev => prev.map((obj) => ({
...obj,
property_to_update: 'new value'
})));
reactibot
reactibot2y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1063585356592717865