Skip to content

Instantly share code, notes, and snippets.

@thegitfather
Created September 10, 2019 09:51
Show Gist options
  • Save thegitfather/7afd5a29c88c64da5d04caa9d81b1b30 to your computer and use it in GitHub Desktop.
Save thegitfather/7afd5a29c88c64da5d04caa9d81b1b30 to your computer and use it in GitHub Desktop.
use reduce() to calc sum of some property in object[]
calcSum = (items, prop) => {
return items.reduce((a, b) => a + b[prop], 0);
}
let foo = [{price: 1}, {price: 2}, {price: 3}];
calcSum(foo, 'price'); // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment