我已经有对象
var info = {fname: '', lname: ''}
我想在info中添加新的属性类型:'abc',这样最终结果将是
{fname: '', lname: '', type: 'abc'}
答案 0 :(得分:2)
var info = {fname: '', lname: ''}
info.type = 'abc';
console.log(info);
答案 1 :(得分:0)
答案 2 :(得分:-1)
最简单的解决方案是:
var info = {fname: '', lname: ''};
info = {...info, ...{type: 'abc'}};
或使用Object.assign:
var result = Object.assign(info, {type: 'abc'});
{fname:'',lname:'',键入:'abc'}
答案 3 :(得分:-1)
很简单:
var info = {fname: '', lname: ''}
info['type'] = 'abc';
有关JSON对象的更多信息: https://www.w3schools.com/js/js_json_objects.asp