# Help with function

**URL:** <https://community.weweb.io/t/help-with-function/781>\
**Category:** How do I?\
**Created:** [July 13, 2022, 6:06pm UTC](https://community.weweb.io/t/help-with-function/781 "2022-07-13T18:06:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anna.fae](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/anna.fae/32/776_2.png) [@Anna.fae](https://community.weweb.io/u/Anna.fae)\
**Post date:** [July 13, 2022, 6:06pm UTC](https://community.weweb.io/t/help-with-function/781/1 "2022-07-13T18:06:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevinwasie](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/kevinwasie/32/402_2.png) [@kevinwasie](https://community.weweb.io/u/kevinwasie)\
**Post date:** [July 14, 2022, 12:53am UTC](https://community.weweb.io/t/help-with-function/781/2 "2022-07-14T00:53:32Z")

</div>

Use custom javascript function:

```auto
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](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/1X/34159f536f920b1b96ba2d3d19b85d88085895bd.png)

---

<div class="post-metadata">

**Author:** ![Anna.fae](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/anna.fae/32/776_2.png) [@Anna.fae](https://community.weweb.io/u/Anna.fae)\
**Post date:** [July 14, 2022, 4:30am UTC](https://community.weweb.io/t/help-with-function/781/3 "2022-07-14T04:30:33Z")

</div>

wow thanks I will try!

---

<div class="post-metadata">

**Author:** ![aurelie](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/aurelie/32/13249_2.png) [@aurelie](https://community.weweb.io/u/aurelie)\
**Post date:** [July 14, 2022, 7:37am UTC](https://community.weweb.io/t/help-with-function/781/4 "2022-07-14T07:37:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![khairul](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@khairul](https://community.weweb.io/u/khairul)\
**Post date:** [July 27, 2022, 1:34am UTC](https://community.weweb.io/t/help-with-function/781/5 "2022-07-27T01:34:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevinwasie](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/kevinwasie/32/402_2.png) [@kevinwasie](https://community.weweb.io/u/kevinwasie)\
**Post date:** [July 27, 2022, 1:53am UTC](https://community.weweb.io/t/help-with-function/781/6 "2022-07-27T01:53:12Z")

</div>

@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”

---

<div class="post-metadata">

**Author:** ![khairul](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@khairul](https://community.weweb.io/u/khairul)\
**Post date:** [July 27, 2022, 2:40am UTC](https://community.weweb.io/t/help-with-function/781/7 "2022-07-27T02:40:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevinwasie](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/kevinwasie/32/402_2.png) [@kevinwasie](https://community.weweb.io/u/kevinwasie)\
**Post date:** [July 27, 2022, 3:32am UTC](https://community.weweb.io/t/help-with-function/781/8 "2022-07-27T03:32:35Z")

</div>

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.
