Skip to content

Instantly share code, notes, and snippets.

@henrik1
Created June 2, 2022 18:45
Show Gist options
  • Select an option

  • Save henrik1/d7c65c361def73245d83002e6ae3b89e to your computer and use it in GitHub Desktop.

Select an option

Save henrik1/d7c65c361def73245d83002e6ae3b89e to your computer and use it in GitHub Desktop.
Calculate the sum of all elements given by a valuating function
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