var data =
[
{
"amount": 270,
"xlabel": "25-31/10",
"datestatus": "past",
"color": "#E74C3C",
"y": 270,
"date": "2020-10-31T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 100,
"xlabel": "25-31/10",
"datestatus": "past",
"color": "#E74C3C",
"y": 100,
"date": "2020-10-30T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
...
]
function getFormationgData(data) {
var sum = [];
let counts = data.reduce((prev, curr) => {
let amount = prev.get(curr.xlabel) || 0;
prev.set(curr.xlabel, curr.amount + amount, curr.entityId, curr.entityName);
return prev;
}, new Map());
// then, map your counts object back to an array
let reducedObjArr = [...counts].map(([xlabel, amount, entityId,
entityName]) => ({
xlabel, amount,
entityId,
entityName
}))
//let reducedObjArr = [...counts].map(data => data)
return reducedObjArr;
}
var pastData = getFormationgData(data.filter(w=>w.datestatus == 'past'));
var futureData = getFormationgData(data.filter(w=>w.datestatus == 'future'));
console.log(pastData, 'pastData', pastData.length)
console.log(futureData, 'futureData', futureData.length)
I'm attempting to calculate the sum of duplicate amounts in an array based on two other properties and retrieve separate arrays for past and future data.
The current dataset is as follows:
Assuming today's date is November 3, 2020
[
{
"amount": 270,
"xlabel": "01-07/11",
"datestatus": "past",
"color": "#E74C3C",
"y": 270,
"date": "2020-02-11T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
...
]
I am trying to organize this data based on `xlabel`, status, and entity. The expected result should contain separate arrays for past and future entries with all corresponding properties intact.
Although I've made some progress, I'm facing difficulties returning all properties in the object. Any assistance would be greatly appreciated!