title: Get values from an object (with arrays) using a string
body: <p>I found this code (<a href="https://stackoverflow.com/a/8817473/1778465">https://stackoverflow.com/a/8817473/1778465</a>), which works nicely, but when I want to get a value from an array such as item 1 I get <code>undefined</code>, I am not really sure what I can do to get array items as well. Any ideas?</p>
  
  <p>This is the code:</p>
  
  <pre><code>var obj = {
      foo: { bar: {animals: [{name: "Billy"},{name: "Bob"},{name: "Joe"}]}}
  };
  
  var deep_value = function(obj, path){
      for (var i=0, path=path.split('.'), len=path.length; i&lt;len; i++){
          obj = obj[path[i]];
      };
      return obj;
  };
  
  console.log(deep_value(obj, 'foo.bar.animals[1].name'));  // Should show "Bob"
  </code></pre>
  
  <p>The above gives me the following error:</p>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot read property 'name' of undefined</p>
  </blockquote>
  
  <p><a href="http://jsfiddle.net/x6h8c57x/" rel="nofollow noreferrer">Fiddle Found Here</a></p>