方法1:声明动态对象
添加属性
//创建obj对象 var obj = new Object(); //为对象添加动态属性 obj.userName = "admin"; obj.passWord = "123456"; //输出 console.log(obj);
方法2:声明对象
//创建固定对象 var dt = {userName:"admin",password:"123456"}; console.log(dt);
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={
...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)