PricingBlog

Update existing object in array on change event

  • tomthebigtree-1275438808069963890

    Tom Ireland

    1 year ago

    I know I have definitely done this before but I cannot figure it out. I have an empty array in a variable. Upon change of a (repeat) input value (change event), I append an object to the array with the id of the repeat element and the updated value e.g. [{"id":1,"name":"some value"}]

    The problem is, when I change the same field again, it appends another object (expected) e.g. [{"id":1,"name":"some value"}, {"id":1,"name":"some other value"}]

    What I need to do is append the object only if the array is empty or if the id of the item being updated is not in the array; otherwise just update the name key value of the object if it already exists.

    As I say, I'm pretty sure I have done this before but cannot for the life of me remember how I did it.

    Any advice gratefully received. 🫣
    1
  • lucasg-1275466121390067712

    Lucas G

    1 year ago

    Sounds like a mix of If index exists then Set the new object otherwise Append
  • Set node works on both objects and arrays and is what we use to set a new value
  • tomthebigtree-1275548009098248283

    Tom Ireland

    1 year ago

    Thanks, @Lucas G - I was thinking something along these lines. The only problem is that depending on which item is updated first, the index of the item is not going to always match the index of the item to be updated in the array.
  • lucasg-1275548179189858478

    Lucas G

    1 year ago

    Then use find index of item no?
  • tomthebigtree-1275549937978966071

    Tom Ireland

    1 year ago

    Here's an example. I have an input repeated based on this array: [{"id":1,"input1"},{"id":2,"input2"},{"id":3,"input3"}]. I change "input2" to "input-2" and append to array in variable, which is [{"id":2,"input-2"}]. The index of the original item is 1. The index of the item in the variable array is now 0. I need away to find it by id I think.
  • lucasg-1275550358331985940

    Lucas G

    1 year ago

    The index doesn't need to change
  • You're updating the item at that index
  • No need to append new item
  • However, if you need to use the ID (which isn't a bad idea either), you can use find index of node
  • tomthebigtree-1275552165624287242

    Tom Ireland

    1 year ago


    You sure? The input is repeated based on the records fetched via the API, so I'm preparing a new array of objects to upsert in a separate variable. I'm not updating the source array of objects because that is just the results from the API call and not set anywhere e.g. in a variable.

    Not sure I've used index of node before.
  • tomthebigtree-1275552652352159787

    Tom Ireland

    1 year ago


    I could store this in a variable when the results are returned and then just update that list probably but then got to think about getting rid of extra items not updated. They'll not change anything but I'd prefer to only provide records that actually need to be updated.
  • lucasg-1275552945542533191

    Lucas G

    1 year ago

    Ah I see, so the new array of items to update would only contain the edited item
  • Got it, didn't understand it properly before
  • tomthebigtree-1275553105471209532

    Tom Ireland

    1 year ago

    That's right. Sorry if I didn't make that clearer before now. 😄
  • lucasg-1275553108583387217

    Lucas G

    1 year ago

    Then yeah, the Find Index node should help you
  • lucasg-1275553565527773266

    Lucas G

    1 year ago

    I use it in a similar manner in a few places. I want to update a certain item specifically
  • tomthebigtree-1275553584582492242

    Tom Ireland

    1 year ago

    Okay, so I'm struggling on the input for that. I know it's going to make sense. 😄
  • tomthebigtree-1275579974925029417

    Tom Ireland

    1 year ago

    Almost cracked it, @Lucas G . I can now update the object in the array but just not append, so likely something to do with how I've set it. Will report back.
  • lucasg-1275580477020966964

    Lucas G

    1 year ago

    DM project if you’d like me to take a look
  • Sounds like you’re on it though
  • tomthebigtree-1275584528366309457

    Tom Ireland

    1 year ago

    Fixed! I had to just basically find the index of the item where the new array object id was equal to the item id I updated. If it was greater than or equal to 0, it existed, so update the item (using Find index to specify the path when using Set), else append an object to the array. Works a treat! If there's a way to simplify it, I'm all ears. 😄
  • Brain hurts. Off to bed. Thanks for you help. You pointed me in the right direction. 🙂 🙏 😴
  • tomthebigtree-1275585074212896809

    Tom Ireland

    1 year ago

    Next step to make sure that I don't do anything if I revert values to the original, so need an object of original values to compare against. Might be overkill. 😄