views:

44

answers:

3

EDIT: Thanks Kenny and everyone else who answered similarly. It really was a DOH...So stupid of me not to remember this.

Maybe the answer is so simple it's eluding me...but I know someone here can school me on this hopefully.

So I've got a rather large project that's dealing with tons of very large JSON, Objects, Arrays, etc..And I need a way of dynamically accessing this data without prior knowledge of the actual names of these. I know someobjectname[string] works, but I need [string][string][string] etc etc. In other words, the entire thing has to be completely dynamic.

I know, I know, there's performance issues with this approach and I'm sure there is better methods but I'm inheriting this problem and believe me, it's not an option to change it.

NOW, here's a super-uber over simplified example to prove the underlying problem. I can't find a way without using eval(), which I can't use because the data is NOT coming trusted sources.

In this example, pretend that foo and bar (both the object names and corresponding option values) are NOT able to known until runtime. Let's say for simplicity they're printed w/ your favorite server-side code.

<script>
// Pretend these objects are inserted into
// the DOM dynamically from where ever
// so we don't know the names till runtime
var foo = {
   value : "something"
}

var bar = {
   value : "something else"
}

window.onload = function() {

    function alertValue(option) {
                            // vvvv This is what I can't do
         var selected_object = eval(option.getAttribute("value"));
         var selected_value = selected_object.value;
         alert(selected_value);    
    }

    var option1 = document.getElementById("option1"); 
    var option2 = document.getElementById("option2");  

    option1.onclick = function () {
         alertValue(this);
    }

    option2.onclick = function () {
        alertValue(this);
    }

}

</script>

<html>
    <select>   <!-- Pretend these values are generated at runtime serverside -->
        <option id="option1" value="foo">Foo's value</option>
        <option id="option2" value="bar">Bar's value</option>
    </select>
</html>

Any help would be great. I'm hoping it's a simple "DOH" moment. Please don't rip apart this code or the method cause there's no point. It isn't anywhere near as complex as the real project is. It's just a proof of concept so you get the problem.

A: 

foo.bar and foo["bar"] mean the same thing in Javascript. The latter is used for properties whose names aren't known til runtime or aren't valid as identifiers. You can easily chain this; foo["bar"]["baz"] means the same as foo.bar.baz.

cHao
Yep, this much I knew and even used in my example. It was foo itself that needed to be a dynamic string.Hence: window[string][string][string][etc]Thanks anyways!
Jay
+1  A: 

If you're executing this in the browser, the global context can be referred with window, hence

var selected_object = window[option.getAttribute("value")];

(or I don't understand what your difficulty is.)

KennyTM
Bingo. It was a "DOH" as I expected. So obvious I didn't even think about it.I'd say it's about time for a cocktail. Hopefully you can enjoy one yourself!Thanks a ton.
Jay
+1  A: 

I am not sure if this is what you want, but you can access the objects foo and bar also via window["foo"] and window["bar"].

moxn
It is indeed. I even showed "someobjectname[string]" in my question but didn't remember window[string][string][string][etc] worked.Kenny posted it with an example in context so other people who stumble across this understand it fully, so he wins the "answered" points.But thank you very much for the reminder! This place never fails!
Jay