欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaScript合并对象的2种写法

程序员文章站 2022-04-05 11:12:00
...

JavaScript合并对象的2种写法
第一种:

let obj1 = {a:"A"};
let obj2 = {b:"b"};
let obj3 = {b:"B",c:"C"};
let obj = Object.assign(obj1 ,obj2 ,obj3 );
console.log(obj)    // {a:"A",b:"B",c:"C"}

第二种:

let obj1 = {a:"A"};
let obj2 = {b:"b"};
let obj3 = {b:"B",c:"C"};
let obj = {...obj1, ...obj2, ...obj3};
console.log(obj)    // {a:"A",b:"B",c:"C"}
相关标签: js