Please enable java script to visit.
NOTEBOOK
HOMEPHP / MySQLJS / HTMLWXappPythonC++Blender其他
js 对象中属性名是变量 - NOTEBOOK
js 对象中属性名是变量
JS / HTML
Posted on 2023-03-01
摘要 : 关键是给属性名变量加上中括号
错误的操作
var a = 'hello';
var b = {a:1,b:2,c:3};
console.log(b);

//{a: 1, b: 2, c: 3}


❱ 正确操作,可以看到属性名是hello而不是a


var aa = 'hello';
var b = {[aa]:1,b:2,c:3};
console.log(b);

// {hello: 1, b: 2, c: 3}