Can someone help me understand why the totalAmount shows as 20 when I add a product? Also, why doesn't it increase when I try to increment it? Any insights would be appreciated.
Thank you.
ts.file
productList = [
{ id: 1, name: 'Louis Vuis', price: 10, qtn: 1 },
{ id: 2, name: 'shubert helmet', price: 20, qtn: 1 },
];
productArray: any = [];
totalAmount: number;
ngOnInit() {
this.totalPrice();
}
add(product, idx) {
const found = this.productArray.find(
item => JSON.stringify(item) === JSON.stringify(product)
);
if (found) {
this.productArray[idx].qtn++;
} else {
this.productArray.push(product);
}
}
totalPrice() {
this.productList.forEach(item => {
this.totalAmount = (item.qtn * item.price)
console.log(this.totalAmount);
});
}