title: JavaScript - Using a variable to refer to a property name?
body: <p>I am creating my own object:</p>
  <pre><code>gridObject = new Object();
  </code></pre>
  <p>I am then using jquery to pull the contents of list item tags, which themselves are filled with <p> tags that have specific class names:</p>
  <pre><code>&lt;li row=&quot;1&quot;&gt;&lt;p class=&quot;department&quot; rowitem=&quot;department&quot;&gt;Photography&lt;/p&gt;...&lt;/li&gt;
  </code></pre>
  <p>I am pulling them using this code:</p>
  <pre><code>//make object from results
  gridObject = new Object();
          
  //get all the rows
  var rowlist = $('li[row]');
          
  for(var r=0; r&lt;rowlist.length; r++) {
              
      //make gridObject row element here
              
      //get the row content
      var thisrow = $(rowlist[r]).html();
  
      //get all the p tags
      var rowitems = $(thisrow + 'p.[rowitem]');
              
      //get field name
      for(var ri=0; ri&lt;rowitems.length; ri++) {
      if (r &lt; 2) { //this is temporary just for testing
           var fieldname = $(rowitems[ri]).attr('rowitem');
           var fieldvalue = $(rowitems[ri]).html();
      }
      }
  </code></pre>
  <p>Ia m getting hung up passing this into my object. Two questions. Can an object property be made with a variable name, like so</p>
  <pre><code>griObject.fieldname = fieldvalue;
  </code></pre>
  <p>and can the objects have parent/child relationships such as:</p>
  <pre><code>gridObject.r.fieldname = fieldvalue; 
  </code></pre>
  <p>in this case both r and fieldname would be variables. Or should I just be working associative arrays to achieve something similar?</p>
  <p>This is in answer to a follow up question I posted below: &quot;Is there a <code>print_r</code> equivalent in javascript&quot; - you can use iterator, a bit more typing but does the trick:</p>
  <pre><code>//loop through search data
  var it = Iterator(filteritems); 
  for(var pair in it) { 
      console.log(&quot;key:&quot; + pair[0] + &quot;, value:&quot; + pair[1] + &quot;\n&quot;);
  }
  </code></pre>