What's the best way to update the value of an object within an array
Victor Giron
2 years ago
I have an array with objects, each object has a title and a selected state that is a boolean. I want to change only the "selected" value of one of the objects with an action, what would be the best way to do it?
Stockton
2 years ago
@Victor Giron This is the only way that I've figured out how to do it. Hoping someone else has a better way? https://share.zight.com/yAuBPv8B
🔥2
Victor Giron
2 years ago
What's the best way to update the value of an object within an array
You can even consolidate it into just the one formula! Great explainer video 🙏
🙏1
Stockton
2 years ago
@Lucas G I'm sure that's easier... how would you use map?
gazinhio
2 years ago
The best is to use the Set formula, link to the array and in the Path point to an array, create two elements for the array, one for the index of the item you want to change and the select element enter "selected".
Stockton
2 years ago
@ Do you mean like this? Doing it in 1 action instead of 2 like I did?
Tod
2 years ago
Great energy @Stockton! Your continuous contribution to the toddle Community just made you advance to Community Level 7!
Stockton
2 years ago
@Stockton
gazinhio
2 years ago
You got it
Stockton
2 years ago
Okay, so I just did it in 2 steps instead of 1... this is more concise though.
gazinhio
2 years ago
No actually you don't need the other Set (the left-most one)
Point the Path of the right-most Set to the array of index and then selected and the value is the new value
gazinhio
2 years ago
Hope that makes sense
Stockton
2 years ago
Now we're Talking!
🤯1
This works
Stockton
2 years ago
I didn't know that path could take an array containing the index as 1 item and the property you want to modify as another...
gazinhio
2 years ago
Yep that's it. Yeah you can go as deep as you need using an array as the path, same with Get to get a nested value from an array and/or object.
Victor Giron
2 years ago
This is great! Thank you so much guys
👍1
Stockton
2 years ago
That helps a lot! Thank you so much for teaching me 🙂 I was doing my other way for a few things... time to refactor? lol idk, might be a lot of work for me 😉