Created
September 10, 2019 09:51
-
-
Save thegitfather/7afd5a29c88c64da5d04caa9d81b1b30 to your computer and use it in GitHub Desktop.
use reduce() to calc sum of some property in object[]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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