2 回答

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
問(wèn)題是您缺少以下作業(yè)map:
this.Bill = this.Bill.map...
但是,作為建議,您可以編寫(xiě)一種更實(shí)用的方法:
addToCart(item: SalesScreenItemsModel) {
const itemExists = this.Bill.some(element => element.itemId === item.itemId);
if (itemExists) {
this.Bill = this.Bill.map(element => ({
...element,
itemQuantity: element.itemQuantity + (element.itemId === item.itemId ? 1 : 0)
}));
} else {
this.Bill = [...this.Bill, item];
}
}
對(duì)于remove:
deleteBillItem(itemIndex: number) {
this.Bill = this.Bill.filter((element, index) => index !== itemIndex);
}
另外,Bill這不是數(shù)組/列表的最佳名稱(chēng):)

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊
添加項(xiàng)目時(shí)嘗試將 item.itemQuantity 設(shè)置為 1
addToCart(item: SalesScreenItemsModel) {
// tslint:disable-next-line: variable-name
const itemIndex=this.Bill.find((billItem) => item.itemId == billItem.itemId);
if(itemIndex == -1){
item.itemQuantity=1;
this.Bill.push(item);
return;
}
this.bill[itemIndex].itemQuantity+=1;
}
- 2 回答
- 0 關(guān)注
- 114 瀏覽
添加回答
舉報(bào)