I have an array containing 4 fields and have to group by two properties and aggregate (sum) one of the field from the grouped array.
tried to group by more than one is not working.
the below code I tried to manipulate the array to the expected list, but not sure how to achieve the below expected result using RxJs
from(this.dummyData)
.pipe(
groupBy(
function (e) {
return { category: e.category, manDate: e.manDate }; //is this valid at all?
},
function (e) { return e; }),
mergeMap(group => zip(of(group.key), group.pipe(toArray())))
)
.subscribe( function(result) {
console.log(result);
});
Existing Array:
[{
category : "Printer",
manDate : "02/01/2019",
amount : 90
},
{
category : "Printer",
manDate : "02/01/2019",
amount : 100
},
{
category : "Printer",
manDate : "02/03/2019",
amount : 90
},
{
category : "Printer",
manDate : "02/04/2019",
amount : 90
},
{
category : "Scanner",
manDate : "08/21/2019",
amount : 8
},
{
category : "Scanner",
manDate : "08/21/2019",
amount : 25
},
{
category : "Scanner",
manDate : "08/21/2019",
amount : 20
},
{
category : "Scanner",
manDate : "08/21/2019",
amount : 10
}
];
expected :
[{
category : "Printer",
subCategory : "A",
manDate : "02/01/2019",
amount : 190
},{
category : "Printer",
subCategory : "A",
manDate : "02/03/2019",
amount : 90
},{
category : "Printer",
subCategory : "A",
manDate : "02/04/2019",
amount : 90
},{
category : "Scanner",
subCategory : "A",
manDate : "08/21/2019",
amount : 63
}]
I need anyone's help to acheive this result.