Wayne
Wayne

Reputation: 1290

Referencing keys in a Javascript object

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?

Upvotes: 2

Views: 1310

Answers (2)

davogones
davogones

Reputation: 7409

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

Upvotes: 2

Luca Matteis
Luca Matteis

Reputation: 29267

Like this:

foo[bar]

You use square brackets to reference string key values.

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

Upvotes: 4

Related Questions