我如何基于数组获取用户详细信息

时间:2018-10-10 12:05:25

标签: javascript arrays

有人可以帮我一些代码吗,我有两个数组,一个包含产品ID,另一个包含完整的产品详细信息。我想基于产品ID数组来控制台记录产品,因此产品ID数组将搜索产品详细信息数组。如果找到匹配项,它将打印出整个产品详细信息。这是两个数组:

var product_id = [
  {
    "order_id": "281774",
    "product_id": "203751",

  },
  {
    "order_id": "281774",
    "product_id": "198999",

  },
  {
    "order_id": "281774",
    "product_id": "199291",

  },

  {
    "order_id": "281774",
    "product_id": "256156",

  }
];

第二个数组

var product_details = [
  {
    "id": "219",
    "product_id": "198999",
    "sku": "20148089",
    "prod_name": "Chocolate Mousse 1L",

    "price": "39.99"
  },
  {
    "id": "220",
    "product_id": "199291",
    "sku": "20008307",
    "prod_name": "Medium Carrots 500g",

    "price": "9.99"
  },
  {
    "id": "221",
    "product_id": "204759",
    "sku": "6009207908908",
    "prod_name": "Fresh Spicy Lentil & Vegetable Soup 600g",
    "price": "39.59"
  },
  {
    "id": "222",
    "product_id": "199015",
    "sku": "6009182131643",
    "prod_name": "Bulk Gouda Cheese 900g",
    "price": "77.62"
  },
  {
    "id": "249",
    "product_id": "200051",
    "sku": "6009195203320",
    "prod_name": "Salted Butter 500g",
    "price": "76.95"
  }

]

5 个答案:

答案 0 :(得分:4)

如果要搜索product_id,这是将在Object中返回数据的解决方案,如果要在Array中返回数据,请使用filter()而不是find()。

var search = product_details.find(detail => {
    return detail.product_id === "199291";
});

您的输出将是

{
     id: "220"
     price:"9.99"
     prod_name:"Medium Carrots 500g"
     product_id:"199291"
     sku:"20008307"
 }

答案 1 :(得分:0)

您可以映射每个product_id并返回product_details数组中的第一个匹配项

product_id.map(a=>({...a, ...product_details.find(b=>b.product_id == a.product_id)}))

您还可以删除与.filter(a=>a.id)

不匹配的内容

var product_id = [{
    "order_id": "281774",
    "product_id": "203751",

  },
  {
    "order_id": "281774",
    "product_id": "198999",

  },
  {
    "order_id": "281774",
    "product_id": "199291",

  },

  {
    "order_id": "281774",
    "product_id": "256156",

  }
];

var product_details = [{
    "id": "219",
    "product_id": "198999",
    "sku": "20148089",
    "prod_name": "Chocolate Mousse 1L",

    "price": "39.99"
  },
  {
    "id": "220",
    "product_id": "199291",
    "sku": "20008307",
    "prod_name": "Medium Carrots 500g",

    "price": "9.99"
  },
  {
    "id": "221",
    "product_id": "204759",
    "sku": "6009207908908",
    "prod_name": "Fresh Spicy Lentil & Vegetable Soup 600g",
    "price": "39.59"
  },
  {
    "id": "222",
    "product_id": "199015",
    "sku": "6009182131643",
    "prod_name": "Bulk Gouda Cheese 900g",
    "price": "77.62"
  },
  {
    "id": "249",
    "product_id": "200051",
    "sku": "6009195203320",
    "prod_name": "Salted Butter 500g",
    "price": "76.95"
  }

]

console.log(
  product_id.map(a=>({...a, ...product_details.find(b=>b.product_id == a.product_id)}))
)

答案 2 :(得分:0)

如果您要搜索每个product_id并查看是否存在于product_details中,则可以使用两个嵌套的for循环进行操作。

示例:

for(let i=0; i<product_id.length; i++){
    for(let j=0; j<product_details.length; j++){
      if(product_id[i].product_id === product_details[j].product_id){
        console.log(product_details[j]);
      }
    }
}

我希望我有正确的主意。祝你好运!

答案 3 :(得分:0)

我不确定这是否是最好的方法,但这就是我总是在数组中寻找项目的方式。

function findInArray(productArray, productId){
for(product of productArray){
    if(product.productId === productId){
        return product;
    }
}
}

答案 4 :(得分:0)

我不完全明白你的意思。也许..尝试这个..

product_id.forEach(prod => {
  if (prod) {
    product_details.forEach(detail => {
      if (detail && detail.product_id === prod.product_id) {
        console.log(detail)
      }
    })
  }
})