Javascript - 对象键 - >值 [英] Javascript - object key->value
本文介绍了Javascript - 对象键 - >值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var obj = {
a: "A",
b: "B",
c: "C"
}
console.log(obj.a); // return string : A
但我希望通过这样的变量来获取
but i want to get by through a variable like this
var name = "a";
console.log(obj.name) // but return undefined
如何做到这一点?
推荐答案
对属性的字符串表示使用 []
表示法: / p>
Use []
notation for string representations of properties:
console.log(obj[name]);
否则它正在寻找name属性,而不是a属性。
Otherwise it's looking for the "name" property, rather than the "a" property.
这篇关于Javascript - 对象键 - >值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文