Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 (<code>obj.foo</code>), <strong>and</strong> via <code>[]</code> notation with a string (<code>obj["foo"]</code>):</p> <pre><code>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 </code></pre> <p>You can see how the <code>[]</code> 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 <code>ingredients</code> 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 <code>ingredients</code> property and to instead have the cocktail object directly contain the ingredients, for example:</p> <pre><code>console.log(cocktails["Blue Hawaiian"]["White Rum"].name); // "shot" console.log(cocktails["Blue Hawaiian"]["White Rum"].quantity); // 1 </code></pre> <p>or of course</p> <pre><code>var bh = cocktails["Blue Hawaiian"]; console.log(bh["White Rum"].name); // "shot" console.log(bh["White Rum"].quantity); // 1 </code></pre> <p>There are lots of different ways you can structure your resulting object graph, depending on how you want to access it and your personal style.</p> <p>The component parts are:</p> <ol> <li><p>Creating an object. That's easy:</p> <pre><code>var obj = {}; </code></pre></li> <li><p>Adding a property to an object using dotted notation a property literal:</p> <pre><code>obj.propertyName = value; </code></pre></li> <li><p>Adding a property to an object using bracketed notation with the name of the property coming from a string expression rather than from a literal:</p> <pre><code>obj["propertyName"] = value; // or var s = "propertyName"; obj[s] = value; // or even var s1 = "property", s2 = "Name"; obj[s1 + s2] = value; </code></pre> <p>In your case, you might get the property name from an XML <code>nodeName</code> or <code>nodeValue</code>.</p></li> <li><p>Putting an object inside another object. This is actually just assigning to a property, where the value you're assigning is an object reference:</p> <pre><code>var outer = {}; var inner = {}; inner.foo = "I'm foo"; // Putting the string "I'm foo" on propety `foo` outer.bar = inner; // Putting the `inner` object on the property `bar` console.log(outer.bar.foo); // "I'm foo" </code></pre></li> </ol> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload