How to change a value in an array?

Greetings weweb folks.

I am new to weweb and not much of a coder. I am trying to build a shopping cart array.

Right now I have this array of list of items added to cart, the quantity and other details :
[
{name:product1, price:9.99, quantity: 2}
{name:product2, price:8.99, quantity:3}…
]

I am trying to figure out how I can change/update the quantity, without changing the order of items. Can you guys share how to it? Thank you so much.

You need to use a custom javascript action.

So, you enter code like this if your array variable is called array:

array[index].name = “product4”;

index is the number of the item in the array. Arrays start at 0 position, so the first item is array[0].

You can use dot notation like that to get or set the value.

Do I need to use change variable value or return? Because I used return and get false as a value

Neither. Take out the return unless you want to return something.

Here, it sounds like you just want to set the value.

Oh I see … change the “return a value” to custom javascript:

1 Like

Thanks so much for your help, been figuring this since yesterday :joy:

1 Like

For information, we have developed a way to do it in no code, because lot of people need the feature.
When doing a “Change variable Value”, you will be able to choose a path and/or an index when modifying an array or object.
This will be on the next release :slight_smile:

4 Likes

you guys rock! Thank you so much for this

And its in production !
You can now update array and object partially with the “Change variable value” action

2 Likes