从中获取价值?

时间:2018-06-28 06:35:31

标签: javascript node.js

如果条件为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吗?

2 个答案:

答案 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
   } 
}