views:

634

answers:

2
var foo = { someKey: "someValue" };
var bar = "someKey";

How do I get the value "someValue" using foo and bar? OK, the PHP equivalent:

$foo = array("someKey" => "someValue");
$bar = "someKey";
print $foo[$bar]; // someValue

So... I'm looking for the JS equivalent for the above, except I don't wanna use a JS array. Help please?

+4  A: 

Like this:

foo[bar]

You use square brackets to reference string key values.

foo.someKey is equal to foo["someKey"]

Luca Matteis
I ticked your answer cos you were 1 minute faster.
Wayne Khan
+2  A: 

foo[bar] should do it. In js objects are basically glorified hashtables.

davogones