How to sum up quantity from an array based on same merchant id

hello, i got a question like to know how can i solve this ?

let say this Cart array got a few objects

[ { id : 1, name: Nokia, price: 10, merchant_id : 1, quantity: 3 },
{ id : 2, name: Beer, price: 20, merchant_id : 1, quantity: 1 },
{ id : 3, name: Mercedes, price: 20, merchant_id : 2, quantity: 2 },
{ id : 4, name: Sushi, price: 60, merchant_id : 3, quantity: 35 },
{ id : 5, name: Iphone, price: 10, merchant_id : 4, quantity: 12 },
{ id : 6, name: Sushi, price: 70, merchant_id : 2, quantity: 5 },
]

example :
merchant id 1 got 2 items, and total quantity for this will be 4
merchant id 2 got 2 items, and total quantity for this will be 7
merchant id 3 got 1 items, and total quantity for this will be 35
merchant id 4 got 1 items, and total quantity for this will be 12

Can do i sum up the all the quantities based on merchant id and pushed to a new array with 4 objects of total quantity?

regards and thank you for your teaching :pray: :pray: :pray:

Ugly way (untyped)

const totals={}  

cartArray.forEach(entry=>{

if (typeof totals[entry.merchant_id]==='undefined') {
    totals[merchant_id]={
       itemcount: 1,
        quantitytotal:entry.quantity  
  }
} else {
    totals[merchant_id]={
           itemcount:  totals[merchant_id]['itemcount']+=1,
           quantitytotal:entry:  totals[merchant_id]['quantitytotal']+=entry.quantity,
    }
}

});

console.log('Totals',totals);

hi, thank you for your help,
i tried to test using the method but the answers of quantity is not sum up for merchant 1 and 2,

merchant 1 and 2 totals are not detected (sum up),
instead it becomes 31 (3, 1)
and 25 (2, 5)

merchant 3 and 4 yes its send to totals (no sum up)

cart =
[ { β€œid” : β€œ1”, name: β€œNokia”, price: β€œ10”, merchant_id : β€œ1”, quantity: β€œ3” },
{ β€œid” : β€œ2”, name: β€œBeer”, price: β€œ20”, merchant_id : β€œ1”, quantity: β€œ1” },

{ β€œid” : β€œ3”, name: β€œMercedes”, price: β€œ20”, merchant_id : β€œ2”, quantity: β€œ2” },
{ β€œid” : β€œ6”, name: β€œSushi”, price: β€œ70”, merchant_id : β€œ2”, quantity: β€œ5” },

{ β€œid” : β€œ4”, name: β€œSushi”, price: β€œ60”, merchant_id : β€œ3”, quantity: β€œ35” },

{ β€œid” : β€œ5”, name: β€œIphone”, price: β€œ10”, merchant_id : β€œ4”, quantity: β€œ12” },

]

calculatetotal(){

const totals={}

this.cart.forEach(entry=>{

if (typeof totals[entry.merchant_id]==='undefined') {
    totals[entry.merchant_id]={
       itemcount: 1,
        quantitytotal:entry.quantity  
  }
} else {
    totals[entry.merchant_id]={
           itemcount:  totals[entry.merchant_id]['itemcount']+=1,
           quantitytotal:totals[entry.merchant_id]['quantitytotal']+=entry.quantity,
    }
}

});

console.log(β€˜Totals’,totals);

}

console.log comes out as attached

did i missed out anything or mixed up ?

regards :slightly_smiling_face: :pray:

I think the problem could be that quantitytotal is a string, not a number?

Too difficult to debug from here…

hi! you are correct! as you were replying, i was reading typeof - JavaScript | MDN,

so, i changed to value of quantity from quantity: β€œ2” to quantity:2,
and the total just sums up right now!

later i try to test it using real database instead of dummy see if it works

update you soon.

thank you for your help! :pray: :slightly_smiling_face:

1 Like