narmi2
asked on
JSON Shopping Cart - Part 2
Dear Experts,
Continuing on from here:
https://www.experts-exchange.com/questions/24802700/JSON-Shopping-Cart.html?anchorAnswerId=25550504#a25550504
How difficult is it to increase the quantity of an item based on the id if the JSON data is structured as follows?
var cart = [
{
'id':'001',
'name':'Bone',
'price':1.00,
'qty':1
},
{
'id':'010',
'name':'U2',
'price':2.00,
'qty':2
},
{
'id':'011',
'name':'Trio',
'price':3.00,
'qty':3
}
];
Continuing on from here:
https://www.experts-exchange.com/questions/24802700/JSON-Shopping-Cart.html?anchorAnswerId=25550504#a25550504
How difficult is it to increase the quantity of an item based on the id if the JSON data is structured as follows?
var cart = [
{
'id':'001',
'name':'Bone',
'price':1.00,
'qty':1
},
{
'id':'010',
'name':'U2',
'price':2.00,
'qty':2
},
{
'id':'011',
'name':'Trio',
'price':3.00,
'qty':3
}
];
Or, if you have the ID, it is a bit more work:
function update_quantity(id, qty) {
for (var i = 0; i < cart.length; i += 1) {
if (cart[i].id === id) {
cart[i].qty = qty;
}
}
}
update_quantity('011', 35);
ASKER
How would one determine the index programatically based on the id?
Like this:
function get_index(id) {
for (var i = 0; i < cart.length; i += 1) {
if (cart[i].id === id) {
return i;
}
}
return -1;
}
var item_id = get_index('010');
if (item_id === -1) {
alert('No item found');
}
else {
alert('Found item #' + id);
}
FYI: For all things JSON, visit http://www.json.org
oops - line 16 should be:
alert('Found item #' + item_id);
ASKER
Is it not possible to structure it like this?
var cart = [
'001': {
'name':'Bone',
'price':1.00,
'qty':1
},
'010':{
'name':'U2',
'price':2.00,
'qty':2
},
'011': {
'name':'Trio',
'price':3.00,
'qty':3
}
];
and then check it using something like this?
cart['001'].qty = 25;
var cart = [
'001': {
'name':'Bone',
'price':1.00,
'qty':1
},
'010':{
'name':'U2',
'price':2.00,
'qty':2
},
'011': {
'name':'Trio',
'price':3.00,
'qty':3
}
];
and then check it using something like this?
cart['001'].qty = 25;
You don't need the array then:
var cart = {
"001": {
"name":"Bone",
"price":1.00,
"qty":1
},
"010": {
"name":"U2",
"price":2.00,
"qty":2
},
"011": {
"name":"Trio",
"price":3.00,
"qty":3
}
};
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>> If you do not need the lenght, and the other array methods, then simply use:
I do need the length.
I do need the length.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And ya know - I think I have this one covered. You could at least make reference to my posts instead of copying them.
>>The correct syntax of JSON is
>>{"name":value}
I know.
>>where "name" is a string enclosed in double-quotes
and if you try the name/key without the quotes, let me know if javascript errors out for you! :)
>>{"name":value}
I know.
>>where "name" is a string enclosed in double-quotes
and if you try the name/key without the quotes, let me know if javascript errors out for you! :)
@narmi2:
There's a major oversight on my part with regards to post ID: 2555153. The array needs to have some valid element. This is NOT a valid element:
'001': {
'name':'Bone',
'price':1.00,
'qty':1
}
The correct syntax for that element is:
{
'001': {
'name':'Bone',
'price':1.00,
'qty':1
}
}
So, essentially you are right back where you started. If you make the corrections, then your structure would be EXACTLY the same as what you posted originally. So, my suggestion would be to use the structure suggested in post ID: 25551400.
There's a major oversight on my part with regards to post ID: 2555153. The array needs to have some valid element. This is NOT a valid element:
'001': {
'name':'Bone',
'price':1.00,
'qty':1
}
The correct syntax for that element is:
{
'001': {
'name':'Bone',
'price':1.00,
'qty':1
}
}
So, essentially you are right back where you started. If you make the corrections, then your structure would be EXACTLY the same as what you posted originally. So, my suggestion would be to use the structure suggested in post ID: 25551400.
>>I know.
But that isn't how you showed us in your example.
And thanks for diluting the question.
But that isn't how you showed us in your example.
And thanks for diluting the question.
narmi2: Again, the strong, silent type.
>>then your structure would be EXACTLY the same as what you posted originally
Which, BTW, is my solution to a previous question from narmi2 (which he neglected to mention).
Which, BTW, is my solution to a previous question from narmi2 (which he neglected to mention).
>> where "name" is a string enclosed in double-quotes
> and if you try the name/key without the quotes, let me know if javascript errors out for you! :)
To make clear what hielo was hinting at, section 11.1.5 of the ECMAScript standard (http://www.ecma-international.org/publications/standards/Ecma-262.htm) says that the key doesn't have to be a string. It can be an identifier (think variable name) instead as long as it's a valid variable name; e.g. is not a reserved keyword, doesn't start with a number, and doesn't contain spaces.
> and if you try the name/key without the quotes, let me know if javascript errors out for you! :)
To make clear what hielo was hinting at, section 11.1.5 of the ECMAScript standard (http://www.ecma-international.org/publications/standards/Ecma-262.htm) says that the key doesn't have to be a string. It can be an identifier (think variable name) instead as long as it's a valid variable name; e.g. is not a reserved keyword, doesn't start with a number, and doesn't contain spaces.
Kravimir: I stand corrected. Thanks for the clarification.
>>And ya know - I think I have this one covered. You could at least make reference to my posts instead of copying them.
I would have if I had seen them. You have been here long enough to know that this issue tends to happen a lot.
I would have if I had seen them. You have been here long enough to know that this issue tends to happen a lot.
Don't come the raw prawn with me, mate. You've been here long enough to know how to prevent that from happening.
ASKER
Follow-up question here:
https://www.experts-exchange.com/questions/24812407/JSON-Shopping-cart-Part-3.html
https://www.experts-exchange.com/questions/24812407/JSON-Shopping-cart-Part-3.html
Open in new window