Set a variable variable on an object in javascript
<p>Doing some work with Omniture analytics and want to be able to set some properties via JSON.</p>

<p>Omniture calls look something like this:</p>

<pre><code>s.linkTrackVars = 'eVar37';
s.eVar37='foo';
s.tl(true, 'o');
</code></pre>

<p>I want to write a generic function to take a JSON object and convert it into those variables. AFAIK Omniture doesn't allow you to simply pass it JSON, so this the only method I can think of.</p>

<p>Here's the example of the JSON structure I want to use:</p>

<pre><code>var omniture = {

    "evars":
    [
        {
            "key": "37",
            "value": "foo"
        },

        {
            "key": "32",
            "value": "bar"
        }
    ]

}
</code></pre>

<p>My function to work with this JSON looks like this:</p>

<pre><code>for (i in omniture.evars) {
    var evar = omniture.evars[i];
    window[s.eVar + evar.key] = evar.value;
}
alert(s.eVar37); // alerts "undefined"
</code></pre>

<p>If I do this, it works:</p>

<pre><code>for (i in omniture.evars) {
    var evar = omniture.evars[i];
    window['eVar' + evar.key] = evar.value;
}
alert(eVar37); // alerts "foo"
</code></pre>

<p>It seems that I can't set a variable variable as a property of an object (eg <code>s.eVar37</code> as opposed to <code>evar37</code>). Can anybody think of a nice way of setting these variables automatically?</p>

<p>thanks,
Matt</p>
