如何将多个对象转换为单个数组

时间:2018-10-11 03:57:56

标签: javascript arrays object

有人可以帮我一些代码吗,我有两个数组,一个带有product_ id,另一个带有产品详细信息,我设法从product_id数组中搜索并显示产品详细信息。我想做的是更多的id是一个项目,它应该作为单个数组返回,请查看我的代码,看看我在说什么。如果有人可以帮助我将结果组合到一个数组中

enter image description here

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": "2047590",
    "sku": "6009207908908",
    "prod_name": "Fresh Spicy Lentil & Vegetable Soup 600g",
    "price": "39.59"
  },
  {
    "id": "222",
    "product_id": "1990150",
    "sku": "6009182131643",
    "prod_name": "Bulk Gouda Cheese 900g",
    "price": "77.62"
  },
  {
    "id": "249",
    "product_id": "2000510",
    "sku": "6009195203320",
    "prod_name": "Salted Butter 500g",
    "price": "76.95"
  }

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

2 个答案:

答案 0 :(得分:0)

e应该是一个数组,当找到匹配项时,您应该push到该数组:

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"}],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:"2047590",sku:"6009207908908",prod_name:"Fresh Spicy Lentil & Vegetable Soup 600g",price:"39.59"},{id:"222",product_id:"1990150",sku:"6009182131643",prod_name:"Bulk Gouda Cheese 900g",price:"77.62"},{id:"249",product_id:"2000510",sku:"6009195203320",prod_name:"Salted Butter 500g",price:"76.95"}];

let e = [];
product_id.forEach(prod => {
  product_details.forEach(detail => {
    if (detail.product_id === prod.product_id) {
      e.push(detail);
    }
  })
});

console.log(e);

答案 1 :(得分:0)

您可以制作e数组并将其推入,但是由于两个原因,您想使用声明性方法来制作新数组:

  • 更容易理解,因为底层的逻辑是通过map()reduce()filter()等功能抽象出来的。
  • 在某些情况下(例如,您必须传递回调函数以执行异步操作时),可能不允许更改变量。

您可以使用Array.prototype.map()Array.prototype.filter()根据原始数组创建一个新数组,如下所示:

let 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" }];
let 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": "2047590", "sku": "6009207908908", "prod_name": "Fresh Spicy Lentil & Vegetable Soup 600g", "price": "39.59" }, { "id": "222", "product_id": "1990150", "sku": "6009182131643", "prod_name": "Bulk Gouda Cheese 900g", "price": "77.62" }, { "id": "249", "product_id": "2000510", "sku": "6009195203320", "prod_name": "Salted Butter 500g", "price": "76.95" }]

let product_id_only = product_id.map(id => id.product_id);
let result = product_details.filter(detail => product_id_only.includes(detail.product_id));
console.log(result);

代码说明:

product_id_onlymap()函数定义,该函数根据提供的回调的返回值创建一个新数组,并以数组中的每个元素为参数。因为回调函数获取参数id并返回id.product_id,所以新数组仅由product_id字符串组成。

result是通过filter()函数计算的,该函数传递数组的每个元素并获得布尔值作为返回值。在这种情况下,product_id_only.includes(detail.product_id)是返回值,当detail的{​​{1}}被包含在先前用product_id组成的数组中时为true。

希望对您有帮助!