循环遍历对象数组并添加具有相同ID的对象

时间:2017-11-01 13:48:14

标签: javascript arrays object

我有一个对象数组,如下所示,我想添加product_id相同的那些对象的数量。

[
      {
        "BARKOD": "Pa Detatime",
        "DETAJIM1": "",
        "DETAJIM2": "",
        "DTMODIFIKIM": "2017-10-02T16:06:53.206Z",
        "product_id": "SD13137",
        "KODI": "MX02",
        "KODNJESIA1": "cope",
        "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56",
        "cmimibaze": 0,
        "quantity": 1
      },
      {
        "BARKOD": "Pa Detatime",
        "DETAJIM1": "",
        "DETAJIM2": "",
        "DTMODIFIKIM": "2017-10-02T16:06:53.206Z",
        "product_id": "SD13137",
        "KODI": "MX03",
        "KODNJESIA1": "cope",
        "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56",
        "cmimibaze": 0,
        "quantity": 3
      },
      {
        "BARKOD": "Pa Detatime",
        "DETAJIM1": "",
        "DETAJIM2": "",
        "DTMODIFIKIM": "2017-10-02T16:06:53.206Z",
        "product_id": "SD13141",
        "KODI": "MX02",
        "KODNJESIA1": "cope",
        "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56",
        "cmimibaze": 0,
        "quantity": 1
      }
    ]

所以结束数组应如下所示:

[
      {
        "BARKOD": "Pa Detatime",
        "DETAJIM1": "",
        "DETAJIM2": "",
        "DTMODIFIKIM": "2017-10-02T16:06:53.206Z",
        "product_id": "SD13137",
        "KODI": "MX02",
        "KODNJESIA1": "cope",
        "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56",
        "cmimibaze": 0,
        "quantity": 4
      },
      {
        "BARKOD": "Pa Detatime",
        "DETAJIM1": "",
        "DETAJIM2": "",
        "DTMODIFIKIM": "2017-10-02T16:06:53.206Z",
        "product_id": "SD13141",
        "KODI": "MX02",
        "KODNJESIA1": "cope",
        "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56",
        "cmimibaze": 0,
        "quantity": 1
      }
    ]

我和另一个here一起问过这个问题,但只能解决另一个问题。如果有人可以解决我会非常感激。 谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用reduce,如下所示:

let input = [{"BARKOD": "Pa Detatime","DETAJIM1": "","ETAJIM2": "","DTMODIFIKIM": "2017-10-02T16:06:53.206Z","product_id": "SD13137","KODI": "MX02","KODNJESIA1": "cope","PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56","cmimibaze": 0,"quantity": 1},{"BARKOD": "Pa Detatime","DETAJIM1": "","DETAJIM2": "","DTMODIFIKIM": "2017-10-02T16:06:53.206Z","product_id": "SD13137","KODI": "MX03","KODNJESIA1": "cope","PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56","cmimibaze": 0,"quantity": 3},{"BARKOD": "Pa Detatime","DETAJIM1": "","DETAJIM2": "","DTMODIFIKIM": "2017-10-02T16:06:53.206Z","product_id": "SD13141","KODI": "MX02","KODNJESIA1": "cope","PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56","cmimibaze": 0,"quantity": 1}];

let output = input.reduce(function(res, el) {
  if(res[el.product_id]) {
    res[el.product_id].quantity += el.quantity;
  } else {
    res[el.product_id] = el;
  }
  return res;
}, {});

let outputArr = Object.values(output);

console.log(outputArr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

var data = [{"BARKOD": "Pa Detatime", "DETAJIM1": "", "DETAJIM2": "", "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", "product_id": "SD13137", "KODI": "MX02", "KODNJESIA1": "cope", "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", "cmimibaze": 0, "quantity": 1 }, {"BARKOD": "Pa Detatime", "DETAJIM1": "", "DETAJIM2": "", "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", "product_id": "SD13137", "KODI": "MX03", "KODNJESIA1": "cope", "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", "cmimibaze": 0, "quantity": 3 }, {"BARKOD": "Pa Detatime", "DETAJIM1": "", "DETAJIM2": "", "DTMODIFIKIM": "2017-10-02T16:06:53.206Z", "product_id": "SD13141", "KODI": "MX02", "KODNJESIA1": "cope", "PERSHKRIMARTIKULLI": "Emporio Armani 4097 5574 71 56", "cmimibaze": 0, "quantity": 1 } ]
 var mapObj = {};
 for(var a of data){
    if(mapObj[a["product_id"]]== undefined)
      mapObj[a["product_id"]] = 0;
    mapObj[a["product_id"]] += a["quantity"]
 }
var data2 = [];
for(var a of data){
  if(mapObj[a["product_id"]] == undefined)
     continue;
  a["quantity"] = mapObj[a["product_id"]];
  data2.push(a)
  delete mapObj[a["product_id"]];
}
console.log(data2)