Update existing object in array on change event
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 . 🫣 ✅1Set node works on both objects and arrays and is what we use to set a new value 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 . 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 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 . Got it , didn 't understand it properly before Sounds like you ’re on it though 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 . 🙂 🙏 😴
.jpeg/public)