Skip to content

Instantly share code, notes, and snippets.

@lsbyerley
Last active July 31, 2020 13:45
Show Gist options
  • Save lsbyerley/709df4f38895a27a754eda47c22e6ce7 to your computer and use it in GitHub Desktop.
Save lsbyerley/709df4f38895a27a754eda47c22e6ce7 to your computer and use it in GitHub Desktop.
Calculate total of product prices in cart with reduce
console.clear();
const cart = [
{price: 12, amount: 1},
{price: 7.5, amount: 2},
{price: 8, amount: 4}
];
const total = [...cart].reduce((total, { amount, price }) => {
return (total += amount * price);
}, 0);
console.log(total);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment