PricingDocsAcademy
Bluesky ...
Wed, Dec 11, 11:07 PM

Trouble using the Set formula

  • /attachments/1293796795226394634/1293796795545026660/image.png

    Danny

    2 months ago

    Hi all! I'm hoping someone can help me understand how the set formula works.

    Let's say I have the following array of objects and I want to update the value of Item 2 to 250.
    [
    {
    "id": "123",
    "name": "Item 1",
    "value": 100
    },
    {
    "id": "456",
    "name": "Item 2",
    "value": 200
    },
    {
    "id": "789",
    "name": "Item 3",
    "value": 300
    }
    ]


    I would think that I could do something like the first image, but that returns null I think because it's expecting an object as the first argument. So then if I back up one level, it seems like it's working (second image). However, it's only returning that final object.

    Can someone help me understand how I can get my full array back with just a value in it changed? I'm guessing this is super easy and there's just something I'm missing.
  • Lucas G

    2 months ago

    You're almost there. You can use Set on either the array or the object
  • What you are missing is a path (or path formula)
  • It needs to know which item to set
  • If you're passing in the array, the formula will need to tell it which index and object key to update
  • For example, index 1 -> "value"
  • If you're just passing in the object, then the path is simply "value"
  • Danny

    2 months ago

    Thanks @Lucas G ! What's the format of the path?
  • If I wanted the index 1 -> "value"
  • Danny

    2 months ago

    Everything I try it seems to just want to create a new key
  • Tom Ireland

    2 months ago

    Hey, @Danny . Is the goal to return the whole array of objects with item 2 updated?
  • Danny

    2 months ago

    Yeah
  • Tod

    2 months ago

    Great energy @Danny! Your continuous contribution to the toddle Community just made you advance to Community Level 2!
  • Danny

    2 months ago

    I have this in a variable and really just want to update one nested value
  • /attachments/1293796795226394634/1293805967472988211/image.png

    Max

    2 months ago

    Here you go πŸ™‚
  • /attachments/1293796795226394634/1293806110855397399/toddle.dev_projects_visus_branches_delta_components_test_canvas-width800canvas-height800rightpanelstyleselectionworkflows.hxoPSs.actions.0.data.png

    Tom Ireland

    2 months ago

    Okay. So, if you're overwriting the existing variable with the change, you want to make sure you're copying the array and only updating the one item. As Lucas said, using an index would be helpful, however if you have the id then you can use that as an identifier to get it. Here's a simple example using map (returns a copy of the array) and sets the item in the array.
  • Tom Ireland

    2 months ago

    Max's way uses the index, which is totally possible as well, like Lucas said. You'd just need a way of finding the index.
  • Max

    2 months ago

    If you know the index, you can just use Set
    πŸ’―1
  • Ah, you already mentioned that, sorry @Tom Ireland πŸ˜…
    πŸ˜‚1
  • Tom Ireland

    2 months ago

    All good - heroes to the rescue! 🦸 πŸ˜„
  • Max

    2 months ago

    How many do you need? πŸ˜‰
    πŸ˜‚2
  • Danny

    2 months ago

    This is awesome! Thank you both for showing me a couple different ways πŸ™
    πŸ’ͺ1
    πŸ™Œ1
  • Tom Ireland

    2 months ago

    Aye - there are a few ways to skin a cat.
  • /attachments/1293796795226394634/1293809062760284203/toddle.dev_projects_visus_branches_delta_components_test_canvas-width800canvas-height800rightpanelstyleselectionworkflows.hxoPSs.actions.0.data_1.png

    Tom Ireland

    2 months ago

    Another way is if you know the item id, you can also find the index and follow a similar approach to Max, which is cleaner than using map.
    πŸ’ͺ2
  • Tom Ireland

    2 months ago

    That's useful if you're getting a specific item id e.g. from the server where data is stored and then need to get that out of an array where the index is unknown.
  • Lucas G

    2 months ago

    Nice examples guys
  • That last one is pretty much the one I would use
    πŸ’ͺ1
  • Points if you get the reference...

Stop scrolling. Start building.

toddle is a visual web app builder that rivals custom code β€” but accessible to your entire team!

Try toddle β€” it's free!

Β© Copyright 2024 toddle. All rights reserved.