Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Applying group by and sum on array of objects in javascript

I need some advice on the proper way to attain my goal of applying group by and sum on JSON data.

Some server-side code actually generates a JSON that I have to work with :

[
    {
        "siteDetails": {
            "printerCode": "660103684",
            "siteId": "UTT212303-STB-2040-0003"
        },
        "printingMaterialCode": "400000033",
        "printingQuantity": 400,
        "approved": true
    },
    {
        "siteDetails": {
            "printerCode": "660103684",
            "siteId": "UTT212303-STB-2040-0002"
        },
        "printingMaterialCode": "400000033",
        "printingQuantity": 600,
        "campaignId": "DATAS00002",
    },
    {
        "siteDetails": {
            "printerCode": "660103684",
            "siteId": "UTT212303-STB-2040-0001"
        },
        "printingMaterialCode": "400000034",
        "printingQuantity": 300,
        "campaignId": "DATAS00002",
    }
]

Now, I need to apply some operation (maybe groupby and sum) to get the below result :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        "printingMaterialCode": "400000033",
        "printingQuantity": 1000
        "approvedQuantity": 400
    },
    {
        "printingMaterialCode": "400000034",
        "printingQuantity": 300,
    }
]

Basically, the output is printingQuantity and approvedQuantity SUM per printingMaterialCode.

>Solution :

const myData = [
    {
        "siteDetails": {
            "printerCode": "660103684",
            "siteId": "UTT212303-STB-2040-0003"
        },
        "printingMaterialCode": "400000033",
        "printingQuantity": 400,
        "approved": true
    },
    {
        "siteDetails": {
            "printerCode": "660103684",
            "siteId": "UTT212303-STB-2040-0002"
        },
        "printingMaterialCode": "400000033",
        "printingQuantity": 600,
        "campaignId": "DATAS00002",
    },
    {
        "siteDetails": {
            "printerCode": "660103684",
            "siteId": "UTT212303-STB-2040-0001"
        },
        "printingMaterialCode": "400000034",
        "printingQuantity": 300,
        "campaignId": "DATAS00002",
    }
]

function elaborateMyData(myData) {
  const approvedQuantity = {};
  const printingQuantity = {};
  const myOutput = [];

  myData.forEach((v) => {
    printingQuantity[v.printingMaterialCode] = printingQuantity[v.printingMaterialCode] || 0;
    approvedQuantity[v.printingMaterialCode] = approvedQuantity[v.printingMaterialCode] || 0;

    printingQuantity[v.printingMaterialCode] += v.printingQuantity;

    if (v.approved) {
      approvedQuantity[v.printingMaterialCode] += v.printingQuantity
    }

  })

  Object.keys(printingQuantity).forEach((v) => {
    myOutput.push({
      printingMaterialCode: v,
      printingQuantity: printingQuantity[v],
      approvedQuantity: approvedQuantity[v],
    })
  })

  return myOutput
}

console.log(elaborateMyData(myData))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading