亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

映射數組并根據條件將對象推入另一個數組的問題 Angular

映射數組并根據條件將對象推入另一個數組的問題 Angular

慕哥9229398 2023-10-04 14:27:11
我正在嘗試設置一個收銀員屏幕..基本上我需要一個 addToCart 功能..非常簡單吧?!我面臨一些奇怪的邏輯錯誤。我所做的是單擊一個項目,捕獲它并將其作為參數傳遞給一個函數,該函數依次映射我的 Bill 數組以檢查該項目是否已存在。如果確實如此,它只會將數量加一,否則,它將將該項目推入數組中。一切都運行良好,直到我刪除一個項目并將其重新添加到數組中。它會保留之前的數量,如果它是 5,那么即使在刪除后它仍保留 5。為了更好的解釋,這是我的代碼......這就是我將商品添加到 Bill(購物車)的方式... TypeScriptaddToCart(item: SalesScreenItemsModel) {let itemExists = false;// tslint:disable-next-line: variable-namethis.Bill.map((ele, _index) => {  if (item.itemId === ele.itemId) {    itemExists = true;    ele.itemQuantity = ele.itemQuantity + 1;  }  return ele;});if (itemExists === false) {  this.Bill.push(item);}超文本標記語言<div class="col-xl-3 col-lg-4 col-md-4 col-sm-6 col-xs-12" *ngFor="let item of items"><div class="card mb-3 widget-content bg-arielle-smile item-pic" style="overflow: hidden; padding: 0;"><div class='box'>  <div class='content'>    <div class="widget-content-wrapper text-white content" style="justify-content: center;">      <div class="widget-content-left text-center">        <img src="{{ item.itemPicture}}" alt="Raised image" class="img-fluid" (click)="addToCart(item)">      </div>    </div>  </div></div>刪除功能deleteBillItem(itemIndex: number) {this.Bill.splice(itemIndex, 1);}超文本標記語言<tr *ngFor="let bill of Bill; let i = index">                      <th scope="row" class="text-center">{{i + 1}}</th>                      <td class="text-center">{{bill.itemName}}</td>                      <td class="text-center">{{bill.itemQuantity}}</td>                      <td class="text-center">{{ bill.itemPrice * bill.itemQuantity }}</td>                      <td class="text-center">                        <button class="btn-icon btn-icon-only btn btn-outline-danger" (click)="deleteBillItem(i)"                          style="padding: 1px 6px;">                          <i class="pe-7s-trash btn-icon-wrapper"> </i>                        </button>                      </td>                    </tr>問題是,當我記錄我的物品數組時,物品數量的變化實際上發生在主數組和賬單上......我知道這應該很簡單,這就是為什么它讓我發瘋......
查看完整描述

2 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

問題是您缺少以下作業map:


this.Bill = this.Bill.map...

但是,作為建議,您可以編寫一種更實用的方法:


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];

  }

}

對于remove:


deleteBillItem(itemIndex: number) {

  this.Bill = this.Bill.filter((element, index) => index !== itemIndex);

}

另外,Bill這不是數組/列表的最佳名稱:)


查看完整回答
反對 回復 2023-10-04
?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

添加項目時嘗試將 item.itemQuantity 設置為 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;

}


查看完整回答
反對 回復 2023-10-04
  • 2 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號