I have an array of Orders and I need to group them according to the carrierCode:
const orders = [
{
"depo": "Berlin",
"boxes": 1,
"isCOD": true,
"CODAmount": 45.33,
"carrierCode": "ups",
"sid": "DCAC298A627DF2D1980D23F67F05E8E4",
},
{
"depo": "Leipzig",
"boxes": 2,
"isCOD": false,
"CODAmount": 0,
"carrierCode": "tnt",
"sid": "8BF93B9159742250CA7F73304808E316",
},
{
"depo": "Leipzig",
"boxes": 2,
"isCOD": true,
"CODAmount": 124.00,
"carrierCode": "dhl",
"sid": "0DC1A9BCFA6C5834361AFABBD857CEDD",
},
{
"depo": "Leipzig",
"boxes": 3,
"isCOD": true,
"CODAmount": 415.33,
"carrierCode": "tnt",
"sid": "8BF93B9159742250CA7F73304808E316",
},
{
"depo": "Berlin",
"boxes": 1,
"isCOD": false,
"CODAmount": 0,
"carrierCode": "tnt",
"sid": "0DC1A9BCFA6C5834361AFABBD857CEDD",
}
];
so far I did this:
var groups = orders.reduce((p, c) => {
var code = c.carrierCode;
if (!p.hasOwnProperty(code)) {
p[code] = 0;
}
p[code]++;
return p;
}, {});
console.log(groups);
var countsExtended = Object.keys(groups).map(k => {
return {code: k, orderscount: groups[k]}; });
console.log(countsExtended);
that returns
{ ups: 1, tnt: 3, dhl: 1 }
and
[
{ code: 'ups', orderscount: 1 },
{ code: 'tnt', orderscount: 3 },
{ code: 'dhl', orderscount: 1 }
]
but now I need to group also by depo and 'import' some other values from orders like boxes count, and total COD Amount: practically, and I should get something like this
[
{ code: 'ups', orderscount: 1, depo:'Berlin', boxes: 1, CODAmount: 45.33},
{ code: 'tnt', orderscount: 1, depo:'Berlin', boxes: 1, CODAmount: 0.00},
{ code: 'tnt', orderscount: 2, depo:'Leipzig', boxes: 5, CODAmount: 415.33},
{ code: 'dhl', orderscount: 1, depo:'Leipzig', boxes: 2, CODAmount: 124.00}
]
how can I get this with ES6/ES10?
Thanks