@T.J.Crowder I thought that JavaScript objects were something more than associative arrays because they can be declared as object literal, but also as functions and they contain other functions. Objects can be created with constructors. What am I missing?
–
stivloNov 13 '11 at 14:58

@stivlo: They're associative arrays arranged in a prototype hierarchy and which can be created in a couple of ways (a literal or by calling a constructor function via new), but that doesn't change the fact that they're associative arrays. :-) Separately, objects don't contain functions, they have properties that refer to functions.
–
T.J. CrowderNov 13 '11 at 15:21

2 Answers
2

Then you the obj is a json object you can manipulate with obj["cocktail_list"]["cock_tail"]["name"]
I didn't consider the array here. For array like cock_tail, you will need to check whether it is already in the obj, and push it in if yes.

What you want to do is create an object graph, which you can readily do with a recursive traversal of your XML tree. Every JavaScript object is a map (aka "associative array," but I don't like that term since they aren't arrays). The properties of an object can be accessed via dotted notation with a literal (obj.foo), and via [] notation with a string (obj["foo"]):

var obj = {}; // A blank object
obj.foo = "bar"; // Now it has a property called "foo"
console.log(obj.foo); // Accessing the property via dotted notation and a literal
console.log(obj["foo"]); // You can also use [] notation and a string
var s = "foo";
console.log(obj[s]); // And of course the string can be the result of any expression,
// including coming from a variable

You can see how the [] notation combined with a string name can readily be used as you traverse your structure, to build up a graph. You'd access the result very similarly to your example, just slight differences. I'd probably opt for the map to be keyed by cocktail name, and then have an ingredients property listing the ingredients (either as an array of objects, or just using the ingredient name as a key). But you can opt not to have the ingredients property and to instead have the cocktail object directly contain the ingredients, for example:

Since you already know how to traverse your XML, that should get you going on building up an object graph from the results of that traversal. I'm not writing actual code to do it because, again, there are many decisions to make in terms of how you want to structure your object graph that are entirely up to how you want to do it.

Thanks T.J. for going to the trouble to include all of the information you have, however I am having trouble working out how to put this into practice. For each "cocktail" in my "cocktail_list" I would like an object/array which will look like: [name[shot(key) => quantity(value), shot(key) => quantity(value), shot(key) => quantity(value)] where the values of "name", "shot", and "quantity" are taken from the XML. I will then put each of these objects/arrays inside an array as I need to be able to compare a cocktail the user has made against the cocktails inside this array. Thanks, Nick
–
NickNov 13 '11 at 12:43