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

Grouping nested arrays data by specific key

I have array list as follow:

let data =[
    {
        "id": "05a87dssff-7468-49b1-bae3-0cd06dc22189",
        "details": [
            [
                {
                    "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2",
                    "qty": 4,
                    "name": "chicken burger",
                    "price": 15,
                    "total": "60.00",
                    
                }
            ]
        ],
    },
    {
        "id": "05a87dff-746gf-49b1-bae3-s0cd06dc22189",
        "details": [
            [
                {
                    "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2",
                    "qty": 4,
                    "name": "chicken burger",
                    "price": 15,
                    "total": "60.00",
                    
                }
            ]
        ],
    },
    
    {
        "id": "06129f89-dd80-49dd-bf5d-a12764c23949",
        "details": [
            [
                {
                    "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2",
                    "qty": 4,
                    "name": "Beef burger",
                    "price": 15,
                    "total": "60.00",
                }
            ],
            [
                 {
                    "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2",
                    "qty": 4,
                    "name": "Beef burger",
                    "price": 15,
                    "total": "60.00",
                }
            ],
            [
                {
                    "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2",
                    "qty": 4,
                    "name": "chicken burger",
                    "price": 15,
                    "total": "60.00",
                }
            ]
        ],
    }
]

I am trying to group all the arrays that have same name and then sum the values of the properties that have the same name in one grand total,
so the expected result based on the above array is:

 [{'name':'chicken burger', 'total': 180},{'name':'Beef burger', 'total': 120}]

What i have tried is the following however, it keeps giving me the all array separated not grouped,
here is what i have done so far:

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

    data.map(a => a.details.map(b => b.reduce((d,e)=> {
  const newArr = d;
  if (d.length && d[d.length - 1]['name'] === e['name']) 
      newArr[d.length - 1] =
      {
        //...d[d.length - 1], ...e,
        total: parseFloat(d[d.length - 1].d) + parseFloat(e.total),
      }
    else newArr[d.length] = { ...e }
    
   console.log(newArr)
    return newArr;
},[]) ) );

and here a link to the code

>Solution :

Iterate over the array, using Map to store calculated sum.

let data =[ { "id": "05a87dssff-7468-49b1-bae3-0cd06dc22189", "details": [ [ { "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2", "qty": 4, "name": "chicken burger", "price": 15, "total": "60.00", } ] ], }, { "id": "05a87dff-746gf-49b1-bae3-s0cd06dc22189", "details": [ [ { "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2", "qty": 4, "name": "chicken burger", "price": 15, "total": "60.00", } ] ], }, { "id": "06129f89-dd80-49dd-bf5d-a12764c23949", "details": [ [ { "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2", "qty": 4, "name": "Beef burger", "price": 15, "total": "60.00", } ], [ { "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2", "qty": 4, "name": "Beef burger", "price": 15, "total": "60.00", } ], [ { "Id": "6741c8b3-03bb-4431-9975-df25eae0a5c2", "qty": 4, "name": "chicken burger", "price": 15, "total": "60.00", } ] ] }];

const map = new Map();

data.forEach(
  item => item.details.forEach(
    detail => {
      const {name, total} = detail[0];
      map.has(name) ? map.get(name).total += +total : map.set(name, {name, total: +total})
    }
  )
);

const result = [...map.values()];

console.log(result);
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