Created
August 3, 2021 12:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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