I am trying to sum the unit value by date, and create a new array where there are no duplicate dates. For example, I want to calculate the total of 2015-12-04 00:01:00
. This date has occurred 2 times in the following data, its value is 5
and 6
, which is going to be:
[{date: '2015-12-04 00:01:00', unit: 11}, ... etc]
I have tried arr = results.map(x => x.unit).reduce((a,c) => a + c)
but it only return a single value, not an array.
results = [ { unit: 5, date: '2015-12-04 00:01:00' },
{ unit: 10, date: '2015-12-04 00:01:00' },
{ unit: 5, date: '2015-12-04 00:31:00' },
{ unit: 9, date: '2015-12-04 00:31:00' },
{ unit: 5, date: '2015-12-04 01:01:00' },
{ unit: 10, date: '2015-12-04 01:01:00' },
{ unit: 10, date: '2015-12-04 01:31:00' },
{ unit: 5, date: '2015-12-04 01:31:00' },
{ unit: 10, date: '2015-12-04 02:01:00' },
{ unit: 5, date: '2015-12-04 02:01:00' },
{ unit: 5, date: '2015-12-04 02:31:00' },
{ unit: 9, date: '2015-12-04 02:31:00' },
{ unit: 5, date: '2015-12-04 03:01:00' },
{ unit: 9, date: '2015-12-04 03:01:00' },
{ unit: 5, date: '2015-12-04 03:31:00' },
{ unit: 10, date: '2015-12-04 03:31:00' },
{ unit: 10, date: '2015-12-04 04:01:00' },
{ unit: 5, date: '2015-12-04 04:01:00' }];
arr = results.map(x => x.unit).reduce((a,c) => a + c);
console.log(arr);