-2

I've a cart object and some buttons in the UI. After clicking a button an event handler will be triggered and an id will be passed as parameter. I want to update the cart when a button clicked. if the passed id don't exist in the cart object, it will set the cart[id] value = 1. Otherwise it will increase the previous value.

I have tried the following code but it's not increasing value if the id is already exist. Where is the problem?

  const cart = {}

  const handleButtonClick = (id) => {
    if (cart.id) {
      cart.id += 1;
    } else {
      cart.id = 1;
    }
  };

0 Answers0