im trying to shrink data in nodejs for a diagram, but have no ideas what would be the best way. i would like to create a function which gets the raw data and shrink the total value by X days as an example:
Data:
{ time: '2018-10-29', total: 8 }
{ time: '2018-10-30', total: 9 }
{ time: '2018-10-31', total: 10 }
{ time: '2018-11-1', total: 4 }
{ time: '2018-11-2', total: 21 }
{ time: '2018-11-3', total: 21 }
{ time: '2018-11-4', total: 4 }
{ time: '2018-11-5', total: 7 }
{ time: '2018-11-6', total: 14 }
{ time: '2018-11-7', total: 11 }
{ time: '2018-11-8', total: 15 }
{ time: '2018-11-9', total: 14 }
{ time: '2018-11-10', total: 23 }
{ time: '2018-11-11', total: 13 }
{ time: '2018-11-12', total: 6 }
{ time: '2018-11-13', total: 14 }
{ time: '2018-11-14', total: 17 }
{ time: '2018-11-15', total: 16 }
{ time: '2018-11-16', total: 16 }
{ time: '2018-11-17', total: 21 }
{ time: '2018-11-18', total: 6 }
{ time: '2018-11-19', total: 12 }
{ time: '2018-11-20', total: 11 }
{ time: '2018-11-21', total: 15 }
{ time: '2018-11-22', total: 15 }
{ time: '2018-11-23', total: 14 }
{ time: '2018-11-24', total: 25 }
{ time: '2018-11-25', total: 1 }
if i would like to shrink this data to 10 days it would be 28/10 = Math.round(2.8) so each 3 would be combined and return. but the question is how i get this to code.
thanks for any advice
You could try something like this:
const getDataInBucket = (bucketIndex, data) => data.filter((obj, i) => i >= bucketSize*bucketIndex && i<bucketSize*(bucketIndex+1));
and then you can call it like that:
getDataInBucket(0, data); // first 10 elements
getDataInBucket(1, data); // second 10 elements
getDataInBucket(2, data); // rest 8 elements