js动态往对象添加数据

第一种方法
let obj ={"name":"tom","age":16}
let key = "id";
let value = 2
obj[key] = value;
console.log(obj)

第二种方法,利用扩展运算符,简单又实用
var obj1={"vue":300,"jquery":200};
var obj2={"react":500};
var obj3={undefined
...obj1,
...obj2
}
console.log(obj3)

第三种办法
let obj4 = {a:"1"};
let obj5 = {b:"2"};
let obj6 = {c:"3"};
Object.assign(obj4,obj5,obj6);
console.log(obj4)

posted @ 2021-12-29 10:54  土小狗  阅读(1928)  评论(0编辑  收藏  举报