How to update item in array using dynamic index?

I’m looping through some data and updating an array based on the data.

I might want to update only a specific item in the array.
I know how to find that array’s index.
I know how to update the variable by writing that index in the dot notation.

But that only works if I literally write in the number myself. I can’t figure out how to find the index, save it as a variable, and then call that variable so that it dynamically updates the right item.

Anyone have any thoughts?

This works:

This doesn’t work:

Hi @ErinHWagner

You will want to use the Set filter on your update variable and use the index as the path. I made a quick tutorial in case any else finds it useful too.

Additionally, we will be adding a replace and replace all filter specifically for arrays in the near future to make this even easier.

1 Like

I really appreciate that you made the video. I read the instructions wrong the first time, so the video got me back on the right course :slight_smile:

I need to take this one step further and I’m having trouble figuring out how. Any input would be appreciated.

Here is the data structure I’m working with (each card shows an activity and a list of groups you could do that activity with):

cards: [
    "activity": "Go rock climbing",
      "group_name":"Friends from work"},
     "activity": "Play soccer",
       "group_name":"Friends from work"},
       "group_name":"College roommates"}

It turns out that the group “Neighbors” (id:4) also wants to play soccer, so I need to add an object to the end of the groups array of the card at index 1.

@michael, your previous instructions showed how to replace the entire object at index 1. But how to I replace just a single property of the object at index 1?

I figured out that I could create a new variable that was a copy of that card (variable=“get” card at index), make the changes to it and then use that whole new card to replace the original. Seems to work fine, but I’m still very interested to know if there’s a most elegant way to execute that.

Just to clarify, are you asking how you’d add “neighbors” (id: 4) to the groups array for play soccer? I’m unclear when you say to replace a single property of the object at index 1.

Yes. I want to add


to the groups array for play soccer.

Hey Erin… here’s a video for you on doing just this

PS… this is a great tutorial to post to youtube, but I’m using your specific use case - wanted to check if that’s ok to use?

1 Like

Ah, that is a much more elegant way than I had hacked together. Thank you!

It doesn’t reveal anything sensitive, so you’re welcome to share it. I appreciate you asking.


1 Like