incrementing product quantity in shopping cart using Javascript

  Kiến thức lập trình

i am adding the product in a cart array, and i want to check if the product already exist in that array, instead of adding the same one just increment the quantity value, but it is not working

let cart = [];

const handleProductCart = (productId) => {
  const addedProduct = products.find((product) => product.id === productId);

  if (cart.some((item) => item.id === productId)) {
    cart = cart.map((product) => {
      let quantity = product.quantity;

      return {
        ...product,
        quantity: quantity++,
      };
    });
  } else {
    cart.push({ ...addedProduct, quantity: 1 });
  }

  console.log(cart);
};

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT