Javascript - object key->value

后端 未结 6 1278
没有蜡笔的小新
没有蜡笔的小新 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 18:07

    Use this syntax:

    obj[name]
    

    Note that obj.x is the same as obj["x"] for all valid JS identifiers, but the latter form accepts all string as keys (not just valid identifiers).

    obj["Hey, this is ... neat?"] = 42
    

提交回复
热议问题