I have an observable that emits measurement values with a date for a key. Something like:
{ "date" : "2021-11-01",
"temp" : 23.4,
"hum" : 74.5
}
I need a 7 days running total and average for temp
and hum
. If I would have a a value for each week, I could write:
const weeklyReducer = (accumulator, currentValue, index) => {
const key = Math.floor((index-1)/7);
const workValue = accumulator[key] || {key, temp: 0, hum:0};
workValue.temp = workValue.temp + currentValue.temp;
workValue.hum = workValue.hum + currentValue.hum;
accumulator[key] = workValue;
return accumulator;
}
However I need a running total where the values are accumulated like this:
Running total 1: 1
Running total 2: 1,2
...
Running total 7: 1,2,3,4,5,6,7
Running total 8: 2,3,4,5,6,7,8
Running total 9: 3,4,5,6,7,8,9
Running total 10: 4,5,6,7,8,9,10
How would I design a reducer for this? I'm open for alternative approaches