4

I serially collect information from forms into arrays like so:

list = {"name" : "John", "email" : "[email protected]", "country" : "Canada", "color" : "blue"}; 
identifier = "first_round";
list = {"name" : "Harry", "email" : "[email protected]", "country" : "Germany"}; 
identifier = "second_round";

I want to combine them into something (I may have braces where I need brackets) like:

list_all = { 
"first_round" : 
 {"name" : "John", "email" : "[email protected]", "country" : "Canada", "color" : "blue"} , 
"second_round" : 
{"name" : "Harry", "email" : "[email protected]", "country" : "Germany"} 
 };

so I can access them like:

alert(list_all.first_round.name) -> John

(Note: the name-values ("name", "email", "color") in the two list-arrays are not quite the same, the number of items in each list-array is limited but not known in advance; I need to serially add only one array to the previous structure each round and there may be any number of rounds, i.e. "third-round" : {...}, "fourth-round" : {...} and so on.)

Ultimately, I'd like it to be well-parsed for JSON.

I use the jquery library, if that helps.

asked May 22, 2010 at 2:15
6
  • On a sidenote, if you're enumerating the properties of list_all as in for(var key in list_all), the order of properties is not guaranteed. You may get "second_round" before "first_round" even though "first_round" was defined first. Use an array instead if you want the order to be preserved. See - stackoverflow.com/questions/648139/… Commented May 22, 2010 at 3:31
  • But when is the order of properties on an object important any way? If order is of importance, then Array is the type to use. Commented May 22, 2010 at 12:33
  • @Sean - When you want direct access to elements with a key, but at the same time also want to iterate through all elements in order. It's not absurd to think that someone might want it - Ruby changed the implementation of Hash in 1.9 to guarantee order because someone wanted it. Commented May 24, 2010 at 19:20
  • Only problem is that the ES262-3 spec states that it is implementation dependent whether the iteration is in order or not - you simply cannot rely on this - and so you shouldn't. And I still fail to see the reason why anyone would need to have the order persisted. Commented May 24, 2010 at 19:25
  • I am actually saying the exact same thing - that you shouldn't rely on the order in which properties will be enumerated. As for the second point, why would someone want direct access to contained objects with a key as well as ordered access to all elements, there are many examples. For a macroscopic view, consider this - there are an infinite number of lists where order is important, but at the same time each item in the list has some unique property based on which it can be immediately and unambiguously identified. To represent such structures, we need the order to be persisted. Commented May 24, 2010 at 21:01

2 Answers 2

1

Create list_all as a new object as follows:

var list_all = {};
list_all[identifier_1] = list_1;
list_all[identifier_2] = list_2;
// ...
answered May 22, 2010 at 2:20
2
  • +1 - Perhaps worth noting that for valid json (as OP seems to want), json2 library can be used. JSON.stringify(list_all) json.org/json2.js Commented May 22, 2010 at 2:45
  • All the hours I spent on this--somehow I missed this one: worked perfectly, thanks! Commented May 22, 2010 at 15:44
0

JSON uses object literal notation. The form:

var person = {
 "name": "Douglas Adams"
 "age": 42
};

Is exactly the same (for all intents and purposes) as:

var person = new Object();
person.name = "Douglas Adams";
person.age = 42;

Does that help you?

You can also use

person["age"]

this is the same as

person.age

and to iterate through named properties:

//prints the person.propertyName for all propertyName in person
for (var propertyName in person) {
 alert(person[propertyName]);
}

You can transmit data as a string, using it to interact with the server and converting it into an object, using jQuery. Ex:

var jsonString = "{'name': 'Douglas Adams', 'age': 42}";
jQuery.parseJson(jsonString); //returns this as an object

Search for JSON in the jQuery API docs: http://api.jquery.com/

answered May 22, 2010 at 2:20
0

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.