【发布时间】:2020-11-18 20:41:00
【问题描述】:
这是我的数组:
const [arr, setArr] = React.useState([
{
"id": 1,
"barcode": "8851907264888",
"qty" : 1
},
{
"id": 2,
"barcode": "8857124022072",
"qty": 1
}
]);
这是我的功能:
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[i], x) == true){
let newArr = [...arr];
newArr[i].qty = newArr[i].qty + 1;
setArr(newArr);
}
}
}
我的问题出在for loop,我想检查每个数组索引,如果每个索引包含与x 相同的barcode,我想为该特定索引添加qty + 1 .但这里显示错误为Cannot read property 'some' of undefined
【问题讨论】:
标签: javascript arrays reactjs syntax-error typeerror