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?

2 Answers 2

4

Like this:

foo[bar]

You use square brackets to reference string key values.

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

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

1 Comment

I ticked your answer cos you were 1 minute faster.
2

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

Comments

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.