我正在尝试向对象添加属性,但Dot符号无法处理字符串。
我的对象:
var lists = {
"Cars":{"Ford":false,"Ferarri":false},
"Names":{"John":true,"Harry":false},
"Homework":{"Maths":true,"Science":false,"History":true,"English":true}
}
添加属性:
function add_item() {
var input = "Alfa Romeo";
var command = eval('lists.Cars.' + input + '=false');
}
如何使用Bracket Notation看到它是2D对象?
答案 0 :(得分:1)
不需要eval ..并且blah
在您的示例中未定义。
var lists = {
"Cars":{"Ford":false,"Ferarri":false},
"Names":{"John":true,"Harry":false},
"Homework":{"Maths":true,"Science":false,"History":true,"English":true}
}
function add_item(key, value) {
lists.Cars[key] = value;
}
add_item('Alfa Romeo', true);
console.log(lists);