引用Javascript对象中的键

时间:2022-09-25 08:40:54
var foo = { someKey: "someValue" };
var bar = "someKey";

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

如何使用foo和bar获取值“someValue”?好的,等价的PHP:

$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?

所以...我正在寻找上面的JS等价物,除了我不想使用JS数组。请帮忙?

2 个解决方案

#1


Like this:

foo[bar]

You use square brackets to reference string key values.

您可以使用方括号来引用字符串键值。

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

foo.someKey等于foo [“someKey”]

#2


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

foo [bar]应该这样做。在js对象中基本上是美化哈希表。

#1


Like this:

foo[bar]

You use square brackets to reference string key values.

您可以使用方括号来引用字符串键值。

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

foo.someKey等于foo [“someKey”]

#2


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

foo [bar]应该这样做。在js对象中基本上是美化哈希表。