I am looking to group payments by month into a new array, regardless of the account type given by 3 categories (DEA, ISA, SIPP).
The data I have is structured as follows:
var arr = [
{ 'DEA','1', 'Jan',1266 },
{ 'ISA','1', 'Jan',621 },
{ 'SIPP','1', 'Jan',552 },
{ 'DEA','2', 'Feb',889 },
{ 'ISA','2', 'Feb',921 },
{ 'SIPP','2', 'Feb',901 },
];
The month number (e.g. 1 or 2) in the data is unnecessary for my desired output.
I aim to consolidate and organize the payments by month in the following format within a new array:
var newarr =
[
{ 'Jan',2439 },
{ 'Feb',2711 },
];
I currently have a code snippet that groups by age category and sums up the total amount, but I haven't been able to adapt it successfully to my payment data yet.
var arr = [
{ AGENDADOR: 'AGE270', TOTAL : 6},
{ AGENDADOR: 'AGE270', TOTAL : 3},
{ AGENDADOR: 'AGE203', TOTAL : 5},
{ AGENDADOR: 'AGE028', TOTAL : 9},
];
var totals = arr.reduce(function (r, o) {
(r[o.AGENDADOR])? r[o.AGENDADOR] += o.TOTAL : r[o.AGENDADOR] = o.TOTAL;
return r;
}, {});
console.log(totals);
Any assistance in getting me started with organizing the payments by month would be greatly appreciated. Thank you.
Best regards,
Colin