Easy way to evaluate path-like expressions in Javascript?
<p>If I have a JavaScript object such as:</p>

<pre><code>var x = {foo: 42, bar: {fubar: true}}
</code></pre>

<p>then I can get the value <code>true</code> with <code>var flag = x.bar.fubar</code>. I'd like to be able to separate out and store the path <code>"bar.fubar"</code>, then evaluate it dynamically. Something like:</p>

<pre><code>var paths = ["bar.fubar", ...];
...
var flag = evalPath( x, paths[0] );
</code></pre>

<p>Obviously I could write a simple parser and evaluator for a basic path expression grammar. But under DRY principles I wonder if there's already an existing way to do something like <code>evalPath</code> built-in to JavaScript, or a small library that would do the job? I also anticipate needing array indexes in the path expression in future.</p>

<p><strong>Update</strong> Just to be clear, I'm not asking for code samples - my question is whether there's existing code (built-in or library) I can re-use. Thanks to the contributors below for suggestions of code samples anyway! Note that none of them handle the array index requirement.</p>
