PricingBlog

What's the best way to update the value of an object within an array

  • victoruxui-1191774012263641209

    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?
    1191774012582416484-image.png
  • stockton_f-1191780138128519299

    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
  • victoruxui-1191783459476230194

    Victor Giron

    2 years ago

    What's the best way to update the value of an object within an array
  • victoruxui-1191784384165064744

    Victor Giron

    2 years ago

    This is a great idea, thank you so much for putting this together! I'll take this approach
  • stockton_f-1191784559486963792

    Stockton

    2 years ago

    Sweet! Hope it helps! 🙂
  • lucasg-1191786936839446619

    Lucas G

    2 years ago

    You can also use the map formula.
    Both approaches will work
  • Use the item’s index value to update its object
  • lucasg-1191787809753804850

    Lucas G

    2 years ago

    You can even consolidate it into just the one formula! Great explainer video 🙏
    🙏1
  • stockton_f-1191853286437945485

    Stockton

    2 years ago

    @Lucas G I'm sure that's easier... how would you use map?
  • gazinhio-1191855331962269887

    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_f-1191856234077376694

    Stockton

    2 years ago

    @ Do you mean like this?

    Doing it in 1 action instead of 2 like I did?
  • Tod-1191856235801235486

    Tod

    2 years ago

    Great energy @Stockton! Your continuous contribution to the toddle Community just made you advance to Community Level 7!
  • stockton_f-1191856243887853698

    Stockton

    2 years ago

    1191856243707494531-tutorialz_-_arrays_Toddle_editor_2024-01-02_at_2.30.43_PM.jpg
  • 510243266084601866-@Stockton
    gazinhio-1191856332064694453

    gazinhio

    2 years ago

    You got it
  • stockton_f-1191856468052418661

    Stockton

    2 years ago

    Okay, so I just did it in 2 steps instead of 1... this is more concise though.
  • gazinhio-1191856565909737483

    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-1191858371989610557

    gazinhio

    2 years ago

    Hope that makes sense
    1191858371855388782-Screenshot_2024-01-02_at_21.33.56.png
  • stockton_f-1191859308980359208

    Stockton

    2 years ago

    Now we're Talking!
    1191859308753850479-tutorialz_-_arrays_Toddle_editor_2024-01-02_at_2.43.09_PM.jpg
    🤯1
  • This works
  • stockton_f-1191859817078325329

    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-1191861231854813254

    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.
  • victoruxui-1191861371088941066

    Victor Giron

    2 years ago

    This is great! Thank you so much guys
    👍1
  • stockton_f-1191861378487697488

    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 😉
  • gazinhio-1191861506015514624

    gazinhio

    2 years ago

    If it works, it works
    🤷‍♂️1
  • gazinhio-1191861806541586484

    gazinhio

    2 years ago

    If it worked for you, keep it. If it ain't broke and all that
  • stockton_f-1191861906659627049

    Stockton

    2 years ago

    lol. yeah I know. But this way is more elegant. So I'll consider it for future nested needs.
    👍1
  • lucasg-1191875450801229894

    Lucas G

    2 years ago

    This is it! Good stuff.
    I mentioned map just to bring it to your awareness but I agree that what you did here is the way to go 👍
    🙌1