It's such an easy task, but somehow I'm stuck. I have an array of objects of product items that looks like this:
[{id: 1, price: 30, quantity: 2}, {id: 2, price: 20, quantity: 4}, {id: 3, price: 10, quantity: 2}]
I need to count the total price of all items, which should be: the price of a specific product multiplied by quantity and the same for previous products. Example: (30*2)+(20*4)+(10*2)
My code looks like this:
items.forEach(function (item) {
let sum = item.price * item.quantity;
console.log(sum);
});
The output looks like:
60
80
20
But I need to count the total which should be 160 and render it to the page and somehow I can't figure out the way to do that
let sumoutside of the for loop ... Seriously, your one step away from solving this yourself..reduce((p, { price, quantity }) => p + price*quantity, 0)+anywhere in your code; that's a bad sign if you are trying to compute a sum.let sumoutside of loop.