Skip to content Skip to sidebar Skip to footer

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'); $

Solution 1:

Like this:

foo[bar]

You use square brackets to reference string key values.

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

Solution 2:

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

Post a Comment for "Referencing Keys In A Javascript Object"