skip to Main Content

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

5

Answers


  1. 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)
    Login or Signup to reply.
  2. 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);
    Login or Signup to reply.
  3. let totalPrice = someArr.reduce((total, item) => {
      total + (item.productQuantity * item.productPrice)
    }, 0);
    
    Login or Signup to reply.
  4. 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
    
    Login or Signup to reply.
  5. 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);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search