Skip to content

Instantly share code, notes, and snippets.

@chalu
Created July 18, 2019 17:17
Show Gist options
  • Select an option

  • Save chalu/228ca18a2228e4e122f2cee26357b56f to your computer and use it in GitHub Desktop.

Select an option

Save chalu/228ca18a2228e4e122f2cee26357b56f to your computer and use it in GitHub Desktop.
const sumOf = (data = [], field) => {
return data.reduce((sum, entry) => {
return sum += entry[field] || 0;
}, 0);
};
const averageOf = (data = [], field) => {
return data.reduce((sum = 0, entry, index) => {
const sum += (entry[field] || 0);
if(index === data.length-1) return sum / data.length;
return sum;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment