title: Accessing nested properties in JavaScript
body: <p>I am writing some code to sort the columns in a table. (The table is bound to data via KnockOutJS). In order to help automate this process a bit across different tables and pages, I  have created an object which has the column names, and the sortPropertyName of the object that column is bound to. </p>
  
  <pre><code>self.headers = [
      { title: 'ID', sortPropertyName: 'organizationId' },
      { title: 'Name', sortPropertyName: 'name' },
      { title: 'City', sortPropertyName: '[address].city' },
      { title: 'State', sortPropertyName: '[address].state' },
      { title: 'Phone Number', sortPropertyName: 'phone' }
  ];
  </code></pre>
  
  <p>That sort property is passed to a sort routine, which should then just sort the data based on the sortProperty:</p>
  
  <pre><code>self.sort = function (header, event) {
      var prop = header.sortPropertyName;
      self.people.sort(function (a, b) {
          return a[prop] &lt; b[prop] ? -1 : a[prop] &gt; b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0;
      });
  };
  </code></pre>
  
  <p>(Full credit goes to Ryan Rahlf <a href="http://ryanrahlf.com/sorting-tables-by-column-header-with-knockout-js-part-2/" rel="nofollow">http://ryanrahlf.com/sorting-tables-by-column-header-with-knockout-js-part-2/</a>)</p>
  
  <p>This is working fine for properties that are "root" properties, such as Name and ID, however this object has another object called "Address" that contains info such as the City and State, etc. This is breaking the sort() function because I'm not correctly specifying how to get to the "city" and "state" parameters.</p>
  
  <p>For example, in order to access "Name" we can use <strong>person.name</strong> or <strong>person['name']</strong>. In the case of address however, things get a little more complicated. We can use <strong>person.address.city</strong> or <strong>person['address'].city</strong> or even <strong>person['address']['city']</strong>.</p>
  
  <p>However no combination of formats seems to be working for me in regard to the <strong>sortPropertyName</strong> that I pass to the <strong>sort()</strong> function. "Name" works fine, but "City" and "State" do not. </p>
  
  <p>How can I modify my code so that everything works as expected?</p>