I have an array of objects that have a price property, and I'm trying to sum all the prices in the object. I think what's tripping me up is that it's an array of objects so I'm having a hard time accessing the price property.
Here's what's displaying in the console: 0[object Object][object Object][object Object][object Object][object Object][object Object][object Object]
Here's my code:
const items = [
{ name: 'Bike', price: 100 },
{ name: 'TV', price: 200 },
{ name: 'Album', price: 10 },
{ name: 'Book', price: 5 },
{ name: 'Phone', price: 500 },
{ name: 'Computer', price: 1000 },
{ name: 'Keyboard', price: 25 }
];
const totalPrice = items.reduce((total, curVal) => {
return total + curVal;
}, 0);
Please login or Register to submit your answer