Reputation: 1022
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"
},
{
"amount": 100,
"xlabel": "01-07/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 100,
"date": "2020-11-01T00: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-25T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 10,
"xlabel": "25-31/10",
"datestatus": "past",
"color": "#E74C3C",
"y": 10,
"date": "2020-10-26T00: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-27T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 110,
"xlabel": "08-14/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 110,
"date": "2020-11-09T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 90,
"xlabel": "08-14/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 90,
"date": "2020-11-10T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 200,
"xlabel": "08-14/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 200,
"date": "2020-11-11T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 220,
"xlabel": "08-14/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 220,
"date": "2020-11-14T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 100,
"xlabel": "15-21/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 100,
"date": "2020-11-21T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"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 am trying to do sum of duplicates of amount in array base on two other properties and retrive past, future two arrays
I am getting data as
Assume today is 3-11-2020
[
{
"amount": 270,
"xlabel": "01-07/11", (weekfirst, lastdate and month)
"datestatus": "past",// past, future
"color": "#E74C3C",
"y": 270,
"date": "2020-02-11T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 270,
"xlabel": "01-07/11", (weekfirst, lastdate and month)
"datestatus": "future",// past, future
"color": "#E74C3C",
"y": 270,
"date": "2020-03-11T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 270,
"xlabel": "01-07/11", (weekfirst, lastdate and month)
"datestatus": "future",// past, future
"color": "#E74C3C",
"y": 270,
"date": "2020-04-11T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
}.....]
I am trying to achive based on xlabel, datastatus, entity
I am able to format it based on xlabel, not getting idea how to get two seperate arrays based on status as well
as per current logic it retuing 4 items(total lenght 11)
Please find attached fiddle the way I tried,,
I am expecting function should return twotypes of arrays with all other data properties
my expectation is the current week dates only come under past and future array
pastarray = [] futurearray = []
By Below answer I achied 80% please find snippet,, but it not returning all properties in obj
Can any one help me on this
Upvotes: 1
Views: 81
Reputation: 3589
I hope I understood the task correctly.
I. Sum of duplicates
What does the algorithm do:
Sorts the values from amount
into two separate ARRAYs for past and future.
Removes the unique values from ARRAYs
Calculates the total value of the remaining values in the array.
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"
},
{
"amount": 100,
"xlabel": "01-07/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 100,
"date": "2020-11-01T00: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-25T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 10,
"xlabel": "25-31/10",
"datestatus": "past",
"color": "#E74C3C",
"y": 10,
"date": "2020-10-26T00: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-27T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo HK",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 110,
"xlabel": "08-14/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 110,
"date": "2020-11-09T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 90,
"xlabel": "08-14/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 90,
"date": "2020-11-10T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 200,
"xlabel": "08-14/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 200,
"date": "2020-11-11T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 220,
"xlabel": "08-14/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 220,
"date": "2020-11-14T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
},
{
"amount": 100,
"xlabel": "15-21/11",
"datestatus": "future",
"color": "#2CD9C5",
"y": 100,
"date": "2020-11-21T00:00:00Z",
"entityId": 1,
"entityName": "Lenovo SG",
"bankName": "BNP Paribas Bank",
"buyerName": "Microsoft",
"currency": "USD"
}
];
//////////////////////////////////////////////
function dataCalc(data, z) {
var fu = [];
for (var i = 0; i < data.length; i++) {
if (data[i].datestatus === z) {
fu.push(data[i].amount);
}
}
function noUnique(array) {
var map = new Map();
array.forEach(a => map.set(a, (map.get(a) || 0) + 1));
return array.filter(a => map.get(a) > 1);
}
var fucls = noUnique(fu);
var res = fucls.reduce((a, b) => a + b, 0);
return res;
}
console.log(dataCalc(data, 'past'));
console.log(dataCalc(data, 'future'));
II. Future and Past - seperate arrays
This function returns an array
with only a future
or past
values from data
function dataSort(data, z) {
var fu = [];
for (var i = 0; i < data.length; i++) {
if (data[i].datestatus === z) {
fu.push(data[i]);
}
}
return fu;
}
console.log(dataSort(data, 'past'));
console.log(dataSort(data, 'future'));
Upvotes: 1