title: json nested lookup using string based key in javascript
body: <p>With a javascript json object like this:</p>
  
  <pre><code>var data = {
      blog : {
          title: "my blog",
          logo: "blah.jpg",
      },
      posts : [
          {
              title: "test post",
              content: "&lt;p&gt;testing posts&lt;/p&gt;&lt;br&gt;&lt;p&gt;some html&lt;/p&gt;"
          },
      ]
  }
  
  var lookup = "blog.title" //this gets generated from a template file
  </code></pre>
  
  <p>Now I know you can do something like, but these don't quite do what I need:</p>
  
  <pre><code>console.log(data['blog']);    //works
  console.log(data.blog.title); //works
  console.log(data['blog']['title']); //works, but i dont know the depth of the lookup
  </code></pre>
  
  <p>But I need to be able to do something like the code below because I can't hardcode the structure, it gets generated and stored in lookup each time. Do I have to build this functionality using string cutting and recursion?? I really hope not</p>
  
  <pre><code>console.log(data['blog.title']); //does not work
  console.log(data[lookup]);       //does not work
  </code></pre>
  
  <p>EDIT....</p>
  
  <p>Okay, possibly found a workaround. I don't know if this is safe or recommended practice, so comments on that would be great. Or alternative methods. So combining this with the code above.</p>
  
  <pre><code>var evaltest = "var asdf ="+JSON.stringify(data)+";\n"
  evaltest += "asdf."+lookup
  console.log(eval(evaltest)) //returns correctly "my blog" as expected
  </code></pre>