引用 Javascript 对象中的键 [英] Referencing keys in a Javascript object
本文介绍了引用 Javascript 对象中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var foo = { someKey: "someValue" };
var bar = "someKey";
如何使用 foo 和 bar 获取值someValue"?好的,PHP 等价物:
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
所以...我正在寻找与上述等效的 JS,但我不想使用 JS 数组.请帮忙?
So... I'm looking for the JS equivalent for the above, except I don't wanna use a JS array. Help please?
推荐答案
像这样:
foo[bar]
您使用方括号来引用字符串键值.
You use square brackets to reference string key values.
foo.someKey
等于 foo["someKey"]
这篇关于引用 Javascript 对象中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文