Maher Aldous
Maher Aldous

Reputation: 942

How to calculate objects from an array - javascript

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));

Upvotes: 0

Views: 2879

Answers (5)

sonEtLumiere
sonEtLumiere

Reputation: 4562

I hope this helps

var total = 0;
for(let obj of arr){
  obj.mult = obj.productQuantity * obj.productPrice;
}
for(let obj of arr){
  total += obj.mult;
}
console.log(total);

Upvotes: 0

Tyler Smith
Tyler Smith

Reputation: 174

If I understand correctly, you're trying to get a single number at the end with the total value of all your inventory. Try this:

let productArray = [
 {
  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
 }
]

productArray.reduce((accumulator, { productPrice, productQuantity }) => {
  return accumulator + (productQuantity * productPrice)
}, 0)

// returns 1900

Upvotes: 2

Ahmed Hammad
Ahmed Hammad

Reputation: 3085

let totalPrice = someArr.reduce((total, item) => {
  total + (item.productQuantity * item.productPrice)
}, 0);

Upvotes: 1

mickl
mickl

Reputation: 49945

You can use array.reduce to aggregate the data from an array of objects:

let input = [
 {
  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
 } 
]

let result = input.reduce((acc,cur) => acc + cur.productQuantity * cur.productPrice, 0);

console.log(result);

Upvotes: 1

djcaesar9114
djcaesar9114

Reputation: 2137

You can use map and reduce:

  • map applies a function to all the elements of the array, in this case the multiplication
  • reduce (with the function I use) sums all the elements of the array.

let prods = [
 {
  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
 }
]

var total = prods.map(x=>x.productQuantity*x.productPrice).reduce((a,b)=>a+b)

console.log(total)

Upvotes: 2

Related Questions