Hello I have these objects in an array and I want to get the total of thier prices after multiply each price with it's quantity.
[
{
productId:55,
productName:"Libero",
productImage:"https://www.albadrbakelser.se/en/wp-content/uploads/sites/2/2019/08/albadr-logo-background.jpg",
productQuantity:7,
productPrice:100
},
{
productId:56,
productName:"Nam Libero Tempore",
productImage:"https://www.albadrbakelser.se/en/wp-content/uploads/sites/2/woocommerce-placeholder.png",
productQuantity:8,
productPrice:150
}
]
I want to multiply first productQuantity * productPrice then get the sum of productPrice from all products after multiplication to get the total
That's what I tried so long
const raw = [{a: 1}, {a: 2}, {a: 3}, {b: 4}, {b: 5}];
const calcSums = someArray => someArray
.reduce( (sums, val) => {
Object.keys(val)
.forEach( v =>
sums[`sum-${v}`] = !sums[`sum-${v}`] ? val[v] : sums[`sum-${v}`] + val[v] );
return sums;
}, {} );
console.log(calcSums(raw));
// the method is generic, so this works too (as long as the values are numbers)
const raw2 = [{a: 1, b:3, c:7}, {a: 2}, {a: 3}, {b: 4}, {b: 5}, {c: 9}];
console.log(calcSums(raw2));