# 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

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

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!

1 Like