0

Im trying to use json to store some values from a page and then access these values/variables later in a php file. Now the thing is that I'm new to json and javascript in general and Im struggling for hours to find a solution to a problem that might be stupidly simple for you guys who have experience in this: Now I have this:

var invoice_data = {
"client":[
{
 "client-name" : "",
 "client-address" : "",
 "client-address-2" : "",
 "client-city-state" : "",
 "client-country" : ""
}
],
"shoppingcart":[ 
 {
 "1" : {"item-name":"", "qty":"", "price":"", "discount":"", "subtotal":""}
 }
],
};

So is this inheritance thing im not really getting. So just as I've created "client" object im creating "shoppingcart", now the thing is that when user orders more than an item there should be created another sub-object that would store it's details too.So im assuming like:

"shoppingcart":[ 
 { 
 "1" : { "item-name":"", "price":"", "discount":"" }
 "2" : { "item-name":"", "price":"", "discount":"" }
 }
],

So when I console.log(invoice_data.shoppingcart); I get the "shoppingcart" object, but I cant access invoice_data.shoppingcart.1 or invoice_data.shoppingcart.2, is there any way I can access 1 or 2 sub-object just as access invoice_data.shoppingcart or invoice_data.client?

Thank you.

Edit: I can access it as an array, im not interested in that option. Ideally i'd like to access the sub-object via it's name.

asked Mar 12, 2013 at 23:36

3 Answers 3

1

Remove the array syntax:

Change this:

"client":[
{
 "client-name" : "",
 "client-address" : "",
 "client-address-2" : "",
 "client-city-state" : "",
 "client-country" : ""
}
]

to this:

"client": {
 "client-name" : "",
 "client-address" : "",
 "client-address-2" : "",
 "client-city-state" : "",
 "client-country" : ""
}

The same goes for shopping cart.

answered Mar 12, 2013 at 23:42
Sign up to request clarification or add additional context in comments.

1 Comment

Hey matt thank you alot, you understood exactly what I was looking for. You're a life saver. I dont know who you are, but I will find you, and thank you :) For now Im marking your answer as the right answer.
0

You can't access invoice_data.shoppingcart.1 because 1 is not a valid Javascript variable name. If the key for an object isn't a valid variable name, you have to use array notation: invoice_data.shoppingcart[1].

EDIT: I didn't see it, but as Matt pointed out, you must remove the array syntax as well.

answered Mar 12, 2013 at 23:41

1 Comment

but normally shoppingcart[1] would return the second object right? this was what I was trying to avoid, because I don't want to add extra complexity to it, because once the user removes an item from the shoppint cart, "shoppingcart" will be updated too (remove that row), so with that in mind Im assuming unpredicted results...
0

Your shopping_cart is an array.

You don't want:

shopping_cart : {
 1 : { name : "...", price : "..." },
 2 : { ... }
}

You want:

shopping_cart : [
 { name : "..." },
 { ... }
]

Then you would access that by saying:

shopping_cart[0].name;
shopping_cart[0].price;

Et cetera.

Arrays also start at 0 and not 1. And this isn't inheritance: it's just a collection of things.

answered Mar 12, 2013 at 23:45

1 Comment

Thanks but this was something I wanted to avoid. Having to rely on array indexes. Is there any non-array-ish solution? When I can access the object by it's name not by the index..? :) Thanks.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.