narmi2
asked on
JSON Shopping cart - Part 3
Dear Experts,
Continuing on from here:
https://www.experts-exchange.com/questions/24804282/JSON-Shopping-Cart-Part-2.html
How do I add a "group" to the data, so ids are grouped into groups. Something like this?
var cart = [
"group1" :
[
"001" : { "item" : "item 1", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 2", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 3", "value" : 3, "qty" : 3 }
],
"group2" :
[
"001" : { "item" : "item 4", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 5", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 6", "value" : 3, "qty" : 3 }
],
"group3" :
[
"001" : { "item" : "item 7", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 8", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 9", "value" : 3, "qty" : 3 }
]
];
Will this allow me to do something like this to change a value?
cart["group2].["010"].qty = 25;
Continuing on from here:
https://www.experts-exchange.com/questions/24804282/JSON-Shopping-Cart-Part-2.html
How do I add a "group" to the data, so ids are grouped into groups. Something like this?
var cart = [
"group1" :
[
"001" : { "item" : "item 1", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 2", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 3", "value" : 3, "qty" : 3 }
],
"group2" :
[
"001" : { "item" : "item 4", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 5", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 6", "value" : 3, "qty" : 3 }
],
"group3" :
[
"001" : { "item" : "item 7", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 8", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 9", "value" : 3, "qty" : 3 }
]
];
Will this allow me to do something like this to change a value?
cart["group2].["010"].qty = 25;
Wrong usage:
alert(cart.group2][1]['010'].item); // ==> item 5
Better:
var cart = {
"group1" : {
"001" : { "item" : "item 1", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 2", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 3", "value" : 3, "qty" : 3 }
},
"group2" : {
"001" : { "item" : "item 4", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 5", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 6", "value" : 3, "qty" : 3 }
},
"group3" : {
"001" : { "item" : "item 7", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 8", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 9", "value" : 3, "qty" : 3 }
}
};
alert(cart.group2['010'].item); // ==> item 5
ASKER
What is the [1] for?
Not needed. Refer to http:#a25573958
ASKER
Will this allow me to get the length of a group and the total length of all the items in a group without looping?
cart.group1.length
and cart.length
?
cart.group1.length
and cart.length
?
Nope.
ASKER
Is it possible to modify the JSON structure to allow such counting without looping?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.
ASKER
So can this be done too for the below data?
alert(cart.group2.010.item ); // ==> item 5
var cart = {
"group1" : {
"001" : { "item" : "item 1", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 2", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 3", "value" : 3, "qty" : 3 }
},
"group2" : {
"001" : { "item" : "item 4", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 5", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 6", "value" : 3, "qty" : 3 }
},
"group3" : {
"001" : { "item" : "item 7", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 8", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 9", "value" : 3, "qty" : 3 }
}
};
alert(cart.group2.010.item
var cart = {
"group1" : {
"001" : { "item" : "item 1", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 2", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 3", "value" : 3, "qty" : 3 }
},
"group2" : {
"001" : { "item" : "item 4", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 5", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 6", "value" : 3, "qty" : 3 }
},
"group3" : {
"001" : { "item" : "item 7", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 8", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 9", "value" : 3, "qty" : 3 }
}
};
Wait!
There's a better solution!!
Here is an object that has properties and methods. It frees you from having to know what array index to use, and opens the door into an entirely new shopping cart.
Save the following as an .HTM file and serve it to your browser. If you have any questions, just post them to this thread.
There's a better solution!!
Here is an object that has properties and methods. It frees you from having to know what array index to use, and opens the door into an entirely new shopping cart.
Save the following as an .HTM file and serve it to your browser. If you have any questions, just post them to this thread.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript">
var o_cart = (function() {
var cart = {
"group1" : {
"001" : { "item" : "item 1", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 2", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 3", "value" : 3, "qty" : 3 }
},
"group2" : {
"001" : { "item" : "item 4", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 5", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 6", "value" : 3, "qty" : 3 }
},
"group3" : {
"001" : { "item" : "item 7", "value" : 1, "qty" : 1 },
"010" : { "item" : "item 8", "value" : 2, "qty" : 2 },
"011" : { "item" : "item 9", "value" : 3, "qty" : 3 }
}
};
function group_length(group) {
var L = 0,
x;
for (x in cart[group]) {
L += 1;
}
return L;
}
function get_cart_item(arg) {
var x = cart[arg.name][arg.id];
return [x.item, x.value, x.qty];
}
// Exposed properties
return {
'length' : function(arg) { return group_length(arg); },
'item' : function(arg) { return get_cart_item(arg); },
};
})();
function page_load() {
alert(o_cart.length('group3')); // ==> item 5
alert(o_cart.item({ 'name':'group2', 'id':'010' }));
}
window.onload = page_load;
</script>
</head>
<body>
</body>
</html>
ASKER
Thanks for the additional solution. I will try this out ASAP. Probably tomorrow.
No worries - glad to help.
Open in new window