Created
June 2, 2022 18:45
-
-
Save henrik1/d7c65c361def73245d83002e6ae3b89e to your computer and use it in GitHub Desktop.
Calculate the sum of all elements given by a valuating function
This file contains hidden or 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
| const sumBy = (fn, list) => | |
| list.reduce((prev, next) => prev + fn(next), 0); | |
| sumBy(product => product.price, [ | |
| { name: 'pizza', price: 10 }, | |
| { name: 'pepsi', price: 5 }, | |
| { name: 'salad', price: 5 }, | |
| ]); // = 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment