I have an array with objects and I need to sum the prices from all these objects.
[{"name":"product1","value":3,"price":60},{"name":"product2","value":3,"price":12},{"name":"product3","value":3,"price":24},{"name":"product4","value":1,"price":16}]
I've tried map to get the prices only form the Array, but it doesn't work..
Well, you are gonna need a variable first. Then you iterate through all objects inside the array, adding the price to the variable.
var price = 0;
[
{ name: 'product1', value: 3, price: 60 },
{ name: 'product2', value: 3, price: 12 },
{ name: 'product3', value: 3, price: 24 },
{ name: 'product4', value: 1, price: 16 },
].forEach((val) => {
price += val.price;
});
console.log(price);