Skip to content

Instantly share code, notes, and snippets.

@drblue
Created March 29, 2022 09:13
Show Gist options
  • Save drblue/f4bfb149f1f71216f77a6cc40a265ba4 to your computer and use it in GitHub Desktop.
Save drblue/f4bfb149f1f71216f77a6cc40a265ba4 to your computer and use it in GitHub Desktop.
Array reduce + map
const products = [
{ name: "Product 1", price: 42 },
{ name: "Product 2", price: 20 },
{ name: "Product 3", price: 1337 },
];
const totalValue = products
.filter(product => product.price > 30)
.reduce( (sum, product) => {
return sum + product.price
}, 0 )
console.log("totalValue:", totalValue)
const productNames = products.map(product => `<p>${product.name}</p>`)
console.log("productNames:", productNames)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment