我有一个将项目推入数组列表的表格,但是当用户选择要将更多设备添加到数组中,并且当更改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);
}
}
尝试一下:
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
});