如何将项目推入数组时解决此问题?

问题描述 投票:0回答:1

我有一个将项目推入数组列表的表格,但是当用户选择要将更多设备添加到数组中,并且当更改MODEL OF TYPE时,如果材质相同,代码会将其计数到以前的数组中。

addItem() {
  const found = this.ListOfUsedMaterials.find(item =>
    item.goodId == this.selectedGood.id && item.device == this.deviceTypId);
  if (found) {
    found.quantity += Number(this.item.quantity);
    this.item.quantity = "";
    this.selectedGood = "";
  } else {
    this.ListOfUsedMaterials.push({
      title: this.selectedGood.value,
      quantity: Number(this.item.quantity),
      goodId: this.selectedGood.id,
      unit: this.selectedGood.unit,
      device: this.deviceTypId,
      manufacturer: this.deviceManufacturerId,
      mark: this.deviceMark
    });
    this.onClear(event);
  }
}

form image

javascript angular ionic-framework
1个回答
0
投票

尝试一下:

 this.ListOfUsedMaterials = this.ListOfUsedMaterials.concat({
  title: this.selectedGood.value,
  quantity: Number(this.item.quantity),
  goodId: this.selectedGood.id,
  unit: this.selectedGood.unit,
  device: this.deviceTypId,
  manufacturer: this.deviceManufacturerId,
  mark: this.deviceMark
});
© www.soinside.com 2019 - 2024. All rights reserved.