How to Update an Object's value in Array in Vue js 3
Let's assume that we are working in a shopping cart with Vue js 3, the user adds items to the cart, and the items are stored in an array, once he moves to view the cart he wants to update the quantity of a specific product, so how we can do that?
Update product quantity
So let's assume that the user wants to increment the quantity of the product in the cart you can do it like below:
const incrementQ = (item) => {
let index = this.cartItems.findIndex(product => product.id === item.id);
if(index !== -1) {
this.cartItems[index].quantity += 1;
Swal.fire({
position: 'top-end',
icon: 'success',
title: 'Your item has been updated',
showConfirmButton: false,
timer: 1500
});
}
}