Javascript - object key->value

后端 未结 6 1282
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 17:40
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

6条回答
  •  心在旅途
    2020-12-04 17:40

    Use [] notation for string representations of properties:

    console.log(obj[name]);
    

    Otherwise it's looking for the "name" property, rather than the "a" property.

提交回复
热议问题