0

I have the following array values,

[
{
    "amount": 4.27,
    "month": 1,
    "year": 2017
},
{
    "amount": 2.46,
    "month": 1,
    "year": 2017
},
{
    "amount": 1.5,
    "month": 2,
    "year": 2017
},
{
    "amount": 28.24,
    "month": 2,
    "year": 2017
},
{
    "amount": 8.24,
    "month": 3,
    "year": 2017
},
{
    "amount": 3.65,
    "month": 3,
    "year": 2017
},
{
    "amount": 0.86,
    "month": 3,
    "year": 2017
},
{
    "amount": 0,
    "month": 1,
    "year": 2018
},
{
    "amount": 71.84,
    "month": 2,
    "year": 2018
},
{
    "amount": 26.62,
    "month": 3,
    "year": 2018
}]

that has to be grouped with respect to specific month and year and the final values should be in the following format,

[
{
    "amount": 6.63,
    "month": 1,
    "year": 2017
},
{
    "amount": 29.74,
    "month": 2,
    "year": 2017
},
{
    "amount":12.75,
    "month": 3,
    "year": 2017
},
{
    "amount": 0,
    "month": 1,
    "year": 2018
},
{
    "amount": 71.84,
    "month": 2,
    "year": 2018
},
{
    "amount": 26.62,
    "month": 3,
    "year": 2018
}]

I have referred this example : Group array items using object

But I cant sort out the summing up part and all I get is only the first value from a particular month and year.

Dabber
  • 33
  • 6

3 Answers3

2

Use reduce, Object.values and sort

var fnGetKey = (obj) => JSON.stringify({year:obj.year, month:obj.month}); //get key from object
Object.values(arr.reduce( (a,c) => {
  var key = fnGetKey( c );
  !a[key] ? (a[key] = c) : (a[key].amount += c.amount); //Initialize with c if key doesn't exists in accumulator, else increment amount
  return a; //return accumulator
} ,{})).sort( (a,b) => a.year - b.year || a.month - b.month ); //sort by year and month

Demo

var arr = [{
    "amount": 4.27,
    "month": 1,
    "year": 2017
  },
  {
    "amount": 2.46,
    "month": 1,
    "year": 2017
  },
  {
    "amount": 1.5,
    "month": 2,
    "year": 2017
  },
  {
    "amount": 28.24,
    "month": 2,
    "year": 2017
  },
  {
    "amount": 8.24,
    "month": 3,
    "year": 2017
  },
  {
    "amount": 3.65,
    "month": 3,
    "year": 2017
  },
  {
    "amount": 0.86,
    "month": 3,
    "year": 2017
  },
  {
    "amount": 0,
    "month": 1,
    "year": 2018
  },
  {
    "amount": 71.84,
    "month": 2,
    "year": 2018
  },
  {
    "amount": 26.62,
    "month": 3,
    "year": 2018
  }
];

var fnGetKey = (obj) => JSON.stringify({
  year: obj.year,
  month: obj.month
}); //get key from object


var output = Object.values(arr.reduce((a, c) => {
  var key = fnGetKey(c);
  !a[key] ? (a[key] = c) : (a[key].amount += c.amount);
  return a;
}, {})).sort((a, b) => a.year - b.year || a.month - b.month);

console.log(output);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Thanks for the solution mate , while what I requested for is working. I would like to know why .sort throws this error `[ts] Property 'year' does not exist on type '{}'. any` – Dabber Apr 26 '18 at 07:24
  • @Dabber [mcve] please? What's your input? Does that deserve having another question? – user202729 Apr 26 '18 at 07:30
1

You can use array#reduce to group your data based on month and year and accumulate all amount in an object in a Map. Then extract all the values and sort the result.

const data = [{ "amount": 4.27, "month": 1, "year": 2017 }, { "amount": 2.46, "month": 1, "year": 2017 }, { "amount": 1.5, "month": 2, "year": 2017 }, { "amount": 28.24, "month": 2, "year": 2017 }, { "amount": 8.24, "month": 3, "year": 2017 }, { "amount": 3.65, "month": 3, "year": 2017 }, { "amount": 0.86, "month": 3, "year": 2017 }, { "amount": 0, "month": 1, "year": 2018 }, { "amount": 71.84, "month": 2, "year": 2018 }, { "amount": 26.62, "month": 3, "year": 2018 } ],
    result = [...data.reduce((r,{amount, month, year}) => {
      const key = month + '/' + year;
      r.has(key) ? r.get(key).amount += amount: r.set(key, {amount, month, year});
      return r;
    },new Map).values()]
    .sort((a,b) => a.year - b.year || a.month - b.month);
console.log(result);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
1

For sorted data, you could check the last item and update the value or push a new object to the result set.

var array = [{ amount: 4.27, month: 1, year: 2017 }, { amount: 2.46, month: 1, year: 2017 }, { amount: 1.5, month: 2, year: 2017 }, { amount: 28.24, month: 2, year: 2017 }, { amount: 8.24, month: 3, year: 2017 }, { amount: 3.65, month: 3, year: 2017 }, { amount: 0.86, month: 3, year: 2017 }, { amount: 0, month: 1, year: 2018 }, { amount: 71.84, month: 2, year: 2018 }, { amount: 26.62, month: 3, year: 2018 }],
    grouped = array.reduce((r, { amount, month, year }) => {
        var last = r[r.length - 1];
        if (last && last.year === year && last.month === month) {
            last.amount += amount;
        } else {
            r.push({ amount, month, year });
        }
        return r;
    }, []);

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392