如果条件为true,则使用某些值,它将在Status
中设置Item
,但是如何从json.products
中的匹配值中获取Qty的值?
if (json.products.some(({ Id }) => Id === Item.Id)) {
Item.Status = {
"Name": "Processed"
"Qty": //Get Qty from matched some (from json.products)?
}
}
如果使用some
不是正确的方法,我应该改用forEach吗?
答案 0 :(得分:0)
使用.find()
。
const matchingProduct = json.products.find(({ Id }) => Id === Item.Id);
if (matchingProduct) {
Item.Status = {
Name: "Processed",
Qty: matchingProduct.Qty, // or something.
};
}
答案 1 :(得分:0)
通过传递与find
相同的回调函数,而使用argument
。
let product = json.products.find(({ Id }) => Id === Item.Id);
if(product){
Item.Status = {
"Name": "Processed"
"Qty": product.Qty
}
}