#how to get data from API in angular
8 messages · Page 1 of 1 (latest)
See #how-to-get-help
addToCart(product : list){
if (product?.variant_list[0]?.price) {
let newCartItem: cartItem = Object.assign({}, {...product}, {quantity: 1, price: parseInt(product.variant_list[0].price)});
console.log('newitem', newCartItem)
let indexOfExistingItem = -1;
this.cartList.forEach((item: cartItem, i:number) => {
if (newCartItem.name === item.name) {
indexOfExistingItem = i
}
})
if (indexOfExistingItem > -1) {
let currentQuantity = this.cartList[indexOfExistingItem]?.quantity
let updatedNewItem = Object.assign(newCartItem, {quantity: currentQuantity + 1});
this.cartList[indexOfExistingItem] = updatedNewItem
} else {
this.cartList.push(newCartItem)
}
console.log(this.cartList)
}
}
Please look at the channel suggested by jbnizet.
Okay, So I am creating a new post and deleting this one.
As you like.
There's no problem in keeping this one.
Our suggestion is not to post your question elsewhere. Our suggestion is to ask a good question, by following the guidelines explained in #how-to-get-help . You need to read and understand that.
g!codeblock @somber perch