Skip to content

Instantly share code, notes, and snippets.

@simeg

simeg/fp_js_5.js Secret

Created May 23, 2022 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simeg/190f3a22436cbd751bb5507e7607cdbd to your computer and use it in GitHub Desktop.
Save simeg/190f3a22436cbd751bb5507e7607cdbd to your computer and use it in GitHub Desktop.
FP in JS 5
// A list of fruit, and their price
const fruit = [
{name: 'banana', price: 5},
{name: 'apple', price: 3},
{name: 'pear', price: 7}
];
// We use reduce() to get the "total price of all fruit"
const sumPriceFruit = fruit
.reduce((previous, current) => previous + current.price, 0);
// Log to see the result
console.log(sumPriceFruit); // 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment