My goal is to calculate the total unit value by date and create a new array without duplicate dates. For instance, I want to sum up the values of 2015-12-04 00:01:00
. This particular date appears twice in the dataset with values of 5
and 6
, resulting in:
[{date: '2015-12-04 00:01:00', unit: 11}, ... etc]
I attempted to use
arr = results.map(x => x.unit).reduce((a,c) => a + c)
but it only returns a single value instead of 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);