Trouble using the Set formula

  • dfeinberg5-1293796795226394634

    Danny

    6 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.
    1293796795545026660-
    1293796795952136224-
  • lucasg-1293797610905403412

    Lucas G

    6 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"
  • dfeinberg5-1293800457533395096

    Danny

    6 months ago

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

    Danny

    6 months ago

    Everything I try it seems to just want to create a new key
  • tomthebigtree-1293804118560866325

    Tom Ireland

    6 months ago

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

    Danny

    6 months ago

    Yeah
  • Tod-1293804184843456593

    Tod

    6 months ago

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

    Danny

    6 months ago

    I have this in a variable and really just want to update one nested value
  • max.kayr-1293805967695151175

    Max

    6 months ago

    Here you go πŸ™‚
    1293805967472988211-
  • tomthebigtree-1293806111073243156

    Tom Ireland

    6 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.
    1293806110855397399-
  • tomthebigtree-1293806427625885718

    Tom Ireland

    6 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.kayr-1293806493229125684

    Max

    6 months ago

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

    Tom Ireland

    6 months ago

    All good - heroes to the rescue! 🦸 πŸ˜„
  • max.kayr-1293806748360249387

    Max

    6 months ago

    How many do you need? πŸ˜‰
    πŸ˜‚2
  • dfeinberg5-1293807364981395466

    Danny

    6 months ago

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

    Tom Ireland

    6 months ago

    Aye - there are a few ways to skin a cat.
  • tomthebigtree-1293809062990843945

    Tom Ireland

    6 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.
    1293809062760284203-
    πŸ’ͺ2
  • tomthebigtree-1293809480470892555

    Tom Ireland

    6 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.
  • lucasg-1293810345298755697

    Lucas G

    6 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.