简体   繁体   中英

Loop through array of objects and add objects that have the same id

I have an array of objects as below and I want to add the quantity of those objects whose product_id is the same.

[
      {
        "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
      }
    ]

So the end array should be like below:

[
      {
        "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
      }
    ]

I asked this question along with another one here but was able to solve only the other question. If anyone might solve I'd be very grateful. Thank you.

You could use reduce, like this:

 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; }

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM