Calculate total price from JavaScript array of product objects
const shoppingCart = [
{
"price": 10,
"title": "Item 1"
},
{
"price": 20,
"title": "Item 2"
},
{
"price": 30,
"title": "Item 3"
}
]
const prices = shoppingCart.map((product) => product.price)
const total = prices.reduce((acc, curr) => acc + curr)
const totalPrice = shoppingCart.reduce((acc, curr) => acc + curr.price, 0)
console.log('total: ', {total, totalPrice} )
no comments