Ways to access a JavaScript Object's Property in IE6

Posted by aaronrussell on Stack Overflow See other posts from Stack Overflow or by aaronrussell
Published on 2010-03-23T18:12:40Z Indexed on 2010/03/23 18:13 UTC
Read the original article Hit count: 293

Filed under:
|
|

I have a JavaScript object with some properties. Lets say:

var haystack = {
  foo: {value: "fooooo"},
  bar: {value: "baaaaa"}
};

Now, I want to access one of those properties, but I don't know which one. Luckily, this variable does:

var needle = "foo";

In modern browsers I seem to be able to do the following and it works:

haystack[needle].value;  # returns "fooooo"

But in IE6 it throws a wobbly, haystack[...] is null or not an object.

Is there a way to achieve what I'm trying to achieve in IE6? If so, how so?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about javascriptobject