Skip to content

Instantly share code, notes, and snippets.

@JoBerkner
Created July 23, 2020 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoBerkner/1e60472521cdd22f9502665afc317a8d to your computer and use it in GitHub Desktop.
Save JoBerkner/1e60472521cdd22f9502665afc317a8d to your computer and use it in GitHub Desktop.
Calculating moving average
export default (data, windowSize) => {
newData = [];
for (var i = windowSize - 1; i < data.length; i++) {
const averages = {};
for (stat of ["confirmed", "deaths"]) {
const curWindowData = data.slice(i - windowSize + 1, i + 1);
const average = curWindowData.reduce((acc, cur) => cur[stat] + acc, 0) / windowSize;
const keyName = "avg_" + stat;
averages[keyName] = Math.round(average);
}
newData.push({
...data[i],
...averages
});
};
return newData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment