Calculate total price from JavaScript array of product objects

node v18.11.0
version: 2.0.0
endpointsharetweet
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} )
Loading…

no comments

    sign in to comment