js动态添加对象

// 第一种方法 var obj = { "name": "张三", "age": 18 } var key = "height"; var value = https://www.it610.com/article/175; obj[key] = value; console.log(obj); // {name:"张三", age: 18, height: 175}// 第二种方法 var obj1 = { "name": "张三", "age": 18 } var value = https://www.it610.com/article/175; obj1.height = value; console.log(obj1); // {name:"张三", age: 18, height: 175}// 第三种方法,扩展运算符 var obj1 = { "apple": 100, "pear": 200 }; var obj2 = { "cherry": 300 }; var obj3 = { ...obj1, ...obj2 }; console.log(obj3); // {apple: 100, pear: 200, cherry: 300}// 第四种办法,合并多个对象 Object.assign var obj1 = { a: "1" }; var obj2 = { b: "2" }; var obj3 = { c: "3" }; var obj = Object.assign(obj1, obj2, obj3); console.log(obj); // {a: "1", b: "2", c: "3"} console.log(obj1); // {a: "1", b: "2", c: "3"}//注意目标对象自身也会改变 console.log(obj2); //{b: "2"} console.log(obj3); //{c: "3"}

    推荐阅读