当前位置:网站首页>Learn how to use js to merge two objects into one object assign()

Learn how to use js to merge two objects into one object assign()

2022-07-07 04:02:00 Lingering memory of Yu Fei

Learn to use js Method of merging two objects into one object Object.assign

Object.assign() Method

Object.assign() Method to copy the values of all enumerable properties from one or more source objects to the target object . It will return the target object

Merge objects

var o1 = {
     a: 1 };
var o2 = {
     b: 2 };
var o3 = {
     c: 3 };
 
var obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
console.log(o1);  // { a: 1, b: 2, c: 3 },  Notice that the target object itself changes 

Merge objects with the same properties

var o1 = {
     a: 1, b: 1, c: 1 };
var o2 = {
     b: 2, c: 2 };
var o3 = {
     c: 3 };
 
var obj = Object.assign({
    }, o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
原网站

版权声明
本文为[Lingering memory of Yu Fei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062109041690.html