0

I have this array

const data =[
             {total: 90, date:"2021-11-16T07:20:23.000Z"},
             {total: 10, date:"2021-11-16T07:20:23.000Z"},
             {total: 50, date:"2021-11-17T07:20:23.000Z"}
            ];

i want to sort this array so the objects that have same date i want to sum the values of the total and the number of these objects with same date with new property called count. result should looks like

const newData =[
                { total: 100, date:"2021-11-16T07:20:23.000Z", count:2},
                { total: 50, date:"2021-11-17T07:20:23.000Z", count:1}
              ];
anie
  • 399
  • 5
  • 17
  • 2
    What have you tried so far? SO is not a free coding service. Please see [How to Ask](https://stackoverflow.com/help/how-to-ask) and [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) for more info. – Reyno Nov 17 '21 at 15:11
  • 1
    perhaps something for your reference https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects – Isaac Nov 17 '21 at 15:13

1 Answers1

3

Start with getting an array of unique date occurence with the help of ...new Set(), then loop through those and when looping filter the original array and map the values.

As others have suggested, nobody here will provide you with the complete code, but at least now you have an idea of how to start.

mikegross
  • 1,674
  • 2
  • 12
  • 27