3 回答
TA贡献1830条经验 获得超3个赞
您将对象传递给 hasBarcode() 而不是数组的代码中的错误您也可以这样做
const handleUpdate = () => {
let x = 8851907264888;
let newArr = arr.map((obj) => {
if (obj.barcode && obj.barcode == x) {
obj.qty = obj.qty + 1;
}
return obj;
});
setArr(newArr);
};
TA贡献1797条经验 获得超6个赞
const hasBarcode = (arr, barcode) => arr.some(el => el.barcode === barcode);
const handleUpdate=()=>{
let x = 8851907264888;
for(let i = 0;i < arr.length;i++){
if(hasBarcode(arr, x) == true){
let newArr = [...arr];
newArr[i].qty = newArr[i].qty + 1;
setArr(newArr);
}
}
}
TA贡献2051条经验 获得超10个赞
const hasBarcode = (arr, barcode) => arr.some(el => el.barcode === barcode);
const handleUpdate=()=>{
let x = 8851907264888;
for(let i = 0;i < arr.length;i++){
if(hasBarcode(arr, x) == true){
let newArr = [...arr];
newArr[i].qty = newArr[i].qty + 1;
setArr(newArr);
}
}
}
添加回答
举报