Skip to content

Instantly share code, notes, and snippets.

@luscas
Created August 3, 2021 12:18
Show Gist options
  • Save luscas/154c5c01bd713d8bd8fa5b5e46510c70 to your computer and use it in GitHub Desktop.
Save luscas/154c5c01bd713d8bd8fa5b5e46510c70 to your computer and use it in GitHub Desktop.
Using Array.reduce to sum a property in an array of objects - Total Shopping Cart
const products = [
{
id: 1,
title: "Banho",
description: "Realizamos a higiene completa do seu pet",
quantity: 5,
price: 79.9,
},
{
id: 2,
title: "Vacina V4",
description: "Himunize seu pet de várias doenças",
quantity: 2,
price: 179.9,
},
{
id: 3,
title: "Vacina Antirrábica",
description: "Vacina contra raiva",
quantity: 1,
price: 89.9,
},
]
const totalCart = products.reduce((prev, curr) => (prev + (curr.quantity * curr.price)), 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment