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.

0

3 Answers 3

3

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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"].

1 Comment

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!
2

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.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.