使用 some,如果条件为 true,它将在 Item
中设置 Status
,但如何从 json.products
中匹配的 some 获取 Qty 值?
if (json.products.some(({ Id }) => Id === Item.Id)) {
Item.Status = {
"Name": "Processed"
"Qty": //Get Qty from matched some (from json.products)?
}
}
如果使用 some
不是正确的方法,我应该使用 forEach 吗?
最佳答案
使用find
,通过传递相同的回调函数作为参数
。
let product = json.products.find(({ Id }) => Id === Item.Id);
if(product){
Item.Status = {
"Name": "Processed"
"Qty": product.Qty
}
}
关于javascript - 从某些人身上获取值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51076167/