Help with function

I need to manage adding products to shopping cart

i do look for contain(product & size) in ShoppingCart array
if product is not in ShoppingCart array → i do add(object) to ShoppingCart array, that works
if product is in ShoppingCart array → i want to update object.amount +1

I try different methods and nothing works (setKeyValue, remove current and add new object where amount +1 etc and I have some errors). I understand that i’m doing something wrong)

Is there any best practice to do that in WeWeb easily? Maybe there is example or tutorial somewhere?

Thanks

Use custom javascript function:

variables.ShoppingCart[index].amount = variables.ShoppingCart[index].amount +1

You can get the index using the nocode array function, findIndexByKey

Screen Shot 2022-07-13 at 8.51.09 PM

1 Like

wow thanks I will try!

The index can also be there from the context if you are inside a repeat.
This feature is more and more asked (changing a subpath inside a variable) so I added it to our features to watch

2 Likes

Hi, can you explain how to do the index? I am trying to do the same function ( shopping cart, adding items and quantity to new array) and cannot figure out how

@khairul For arrays, you place the index in in brackets. So, if you have an array like

array = [“jim”,”mike”,”lisa”,”Tom”]

If you put array[1] it would return “mike”

1 Like

thank you, how do I change/update a value inside an array?

array = [{“name”:”Lisa”}, {“name”:”Mike”}];

array[1].name = “John”;

//this code would change the name Mike to John

If you want to lookup stuff like this, search google or stackexchange for things like javascript set array value. You’ll get tons of resources there as well.

W3school is also great for JavaScript tutorials.

1 Like