How do I count the prices together from the following array of objects:
const cars = [
{
price: 10000,
car: 'audi'
},
{
price: 10000,
car: 'audi'
},
{
price: 10000,
car: 'nissan'
},
{
price: 10000,
car: 'nissan'
},
{
price: 20000,
car: 'mazda'
},
{
price: 10000,
car: 'nissan'
},
];
So that I would get an array of objects, with the total prices:
const cars = [
{
price: 20000,
car: 'audi'
},
{
price: 30000,
car: 'nissan'
},
{
price: 20000,
car: 'mazda'
}
];
Tried to map over the array, and then reduce, but how do i keep the structure, so that I get back an array of objects? This is incorrect:
const newCarsArray = cars.map(item => {
return(
//cars.reduce ?
)
});