Skip to content

Instantly share code, notes, and snippets.

@stekhn
Last active April 28, 2020 17:36
Show Gist options
  • Save stekhn/e8b7f5a2b8bcc44d80ec20d68efb0e71 to your computer and use it in GitHub Desktop.
Save stekhn/e8b7f5a2b8bcc44d80ec20d68efb0e71 to your computer and use it in GitHub Desktop.
Calculate the the daily reproduction numbers (R) from an object array
function reproNumber(data, steps = 4, key = 'value') {
return data.map((obj, i) => {
const currentDays = data.slice(i - steps, i);
const previousDays = data.slice(i - steps * 2, i - steps);
const currentDaysSum = currentDays.reduce((sum, curr) => { return sum + curr[key]; }, 0);
const previousDaysSum = previousDays.reduce((sum, curr) => { return sum + curr[key]; }, 0);
return Object.assign({}, obj, {
r: currentDaysSum / previousDaysSum
});
}).filter(d => d.r && isFinite(d.r));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment