Reputation: 31
I have an object within an object where I want to count the quantity and return the value.
Example:
cart: {items: {A {number: 'xxx', quantity: 1, price: 999}, B {number: 'xxx', quantity: 3, price: 999}, C {number: 'xxx', quantity: 2, price: 999} }}
What I now want is the return of the quantity count 6.
This is what I have achieved so far. How do I proceed?
function() {
var value = {{DataWithInformations}}
var itemCount = cart.items.quantity
}
Thank you very much.
Upvotes: 2
Views: 768
Reputation: 10366
A clean ES6 solution:
const cart = {
items: {
A: {number: 'xxx', quantity: 1, price: 999},
B: {number: 'xxx', quantity: 3, price: 999},
C :{number: 'xxx', quantity: 2, price: 999}
}
};
const total = Object.values(cart.items)
.map((obj) => obj.quantity)
.reduce((prev, curr) => prev + curr, 0);
console.log(total);
Upvotes: 0
Reputation: 406
I would use a for
loop for your problem.
I see you put a colon after cart
so I guess your cart is in another JSON object.
var foo = {
cart: {
items: {
A: {number: 'xxx', quantity: 1, price: 999},
B: {number: 'xxx', quantity: 3, price: 999},
C: {number: 'xxx', quantity: 2, price: 999}
}
}
};
// This is what you need
document.getElementById("please-click-me").onclick = function() {
var sum = 0;
// Note that the 'in' gives you the keys, not the objects themselves
for (var key in foo.cart.items) {
sum += foo.cart.items[key].quantity;
}
alert(sum);
}
<button id="please-click-me">Click</button>
Hope this helps :).
Upvotes: 0
Reputation: 26547
There are a couple ways to go about it.
One of the cleanest would would probably be to use the reduce
function:
data.cart.items.reduce((sum, item) => sum + item.quantity, 0);
Written with ES5 (no arrow functions), it'd look like this:
data.cart.items.reduce(function (sum, item) { return sum + item.quantity }, 0);
(Note: What you posted isn't valid JSON or a valid object, so I made some guesses as to how the object actually is.
const data = {
cart: {
items: [
{ quantity: 1 },
{ quantity: 2 },
{ quantity: 3 }
]
}
};
const sum = data.cart.items.reduce((sum, item) => sum + item.quantity, 0);
console.log(sum);
Update to go with the changed object in the question:
ES6:
Object.keys(a.cart.items).reduce((sum, key) => sum + a.cart.items[key].quantity, 0);
ES5:
Object.keys(a.cart.items).reduce(function (sum, key) { return sum + a.cart.items[key].quantity, 0);
const a = {
cart: {
items: {
A: {number: 'xxx', quantity: 1, price: 999},
B: {number: 'xxx', quantity: 3, price: 999},
C :{number: 'xxx', quantity: 2, price: 999}
}
}
};
const result = Object.keys(a.cart.items).reduce((sum, key) => sum + a.cart.items[key].quantity, 0);
console.log(result);
Upvotes: 1
Reputation: 1577
This should be a solution:
a = {cart:{items:{A:{number: 'xxx', quantity: 1, price: 999}, B: {number: 'xxx', quantity: 3, price: 999}, C :{number: 'xxx', quantity: 2, price: 999} }}};
sum=0;
for(key in a.cart.items){
sum += a.cart.items[key].quantity;
}
console.log(sum);
Upvotes: 0
Reputation: 30739
You need to loop over to the JSON and then add the quantity
.
var jsonData = {'cart':
{'items':
{
'A':{'number': 'xxx', 'quantity': 1,'price': 999},
'B' : {'number': 'xxx', 'quantity': 3, 'price': 999},
'C':{'number': 'xxx', 'quantity': 2, 'price': 999}
}
}
};
var keys = Object.keys(jsonData.cart.items);
var quantity = 0;
for(var i=0; i<keys.length; i++){
var key = keys[i];
var item = jsonData.cart.items[key];
quantity += item.quantity;
}
alert(quantity);
Here is the link to JSFIDDLE
Upvotes: 0
Reputation: 4830
You can use the reduce function to get the value out of your Array. PS: The data structure you have provided is not syntactically correct so I'm going to make a few assumptions.
var data = {
cart: {
items: [{
number: 'xxx',
quantity: 1,
price: 999
},
{
number: 'xxx',
quantity: 3,
price: 999
},
{
number: 'xxx',
quantity: 2,
price: 999
}
]
}
};
var qtySum = data.cart.items.reduce(function(mem, item) {
mem += item.quantity;
return mem;
}, 0);
console.log("Total count:", qtySum);
Upvotes: 1