0

I need to create a new array grouping payments by month regardless of the account type given by 3 categories (DEA,ISA,SIPP)

My data is of the form

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 },
      ];

Month No 1 or 2 etc is redundant data, ie. not required in my output

I need to group the payments by month in the following form into a new array:

var newarr =
[
{ 'Jan',2439 },
{ 'Feb',2711 },
];

I have used the following code as my starting point which groups by age category and summates the TOTAL, but I have been unable to apply to my data successfully 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 help much appreciated to get me started, thanks.

Many Thanks

Colin

Ele
  • 33,468
  • 7
  • 37
  • 75
colin
  • 65
  • 2
  • 12
  • 1
    And, what have you tried to solve that? btw, that's an invalid input. – Ele Jul 02 '19 at 12:23
  • 1
    Your data is invalid syntax. – str Jul 02 '19 at 12:24
  • I am new to javascript, I have tried to use the reduce function as shown in another question successfully replicating for a similar array, but I have not been able to apply it to my data due to its multi dimension nature, i will keep trying thanks Colin – colin Jul 02 '19 at 12:25
  • Post those attempts with reduce. – Ele Jul 02 '19 at 12:26
  • var arr = [ { AGENDADOR: 'AGE270', TOTAL : 6}, { AGENDADOR: 'AGE270', TOTAL : 3}, { AGENDADOR: 'AGE203', TOTAL : 5}, { AGENDADOR: 'AGE028', TOTAL : 9}, ], This was my starting point albeit using different data totals = arr.reduce(function (r, o) { (r[o.AGENDADOR])? r[o.AGENDADOR] += o.TOTAL : r[o.AGENDADOR] = o.TOTAL; return r; }, {}); – colin Jul 02 '19 at 12:28
  • 1
    You should rewrite your question and post the code of the last comment. – Ele Jul 02 '19 at 12:30
  • str, is the data invalid because there are no qualifiers, sorry if novice question? – colin Jul 02 '19 at 12:36

1 Answers1

1

In order to sum up the months, an alternative is maintaining an array of months which is used to loop and extract the values you need to sum.

Basically, the function reduce groups the values by month and the function Object.values extracts the grouped values as an array.

This approach will extract as many months are presented in the objects.

let months = ['Jan', 'Feb', 'Mar'];// and so on.
let 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 }      ];
      
let result = Object.values(arr.reduce((a, c) => {
  months.forEach(m => {
    if (m in c) (a[m] || (a[m] = {[m]: 0}))[m] += c[m];
  });
  return a;
}, Object.create(null)));

console.log(result);
.as-console-wrapper { min-height: 100%; }
Ele
  • 33,468
  • 7
  • 37
  • 75