当前位置:网站首页>Four methods of object merging and four methods of object merging in JS
Four methods of object merging and four methods of object merging in JS
2022-06-24 10:37:00 【Yansuizi blog】
Catalog
2、 Use array.concat() Method to merge
4、array.push() Method to merge
One 、 Object to merge
1、 Extension operators (...)
ES6 It is said in the introduction :
Object's extension operator (
...) Used to fetch all traversable properties of parameter object , Copy to current object .
let obj1 = {
name: ' Chen Weiting ',
gender: ' male ',
hobby: ' Sing a song '
};
let obj2 = {
name: ' Chen Weiting ',
gender: ' male ',
hobby: ' dance ',
nationality: ' China '
};
let obj = {...obj1, ...obj2};
console.log(obj); // { name: ' Chen Weiting ', gender: ' male ', hobby: ' dance ', nationality: ' China ' }
As can be seen from the above code :
- Property of the same name : Merge , And the back (obj2) Put the front (obj1) Cover .
- Different name attributes : Property value unchanged , Merge only .
notes : The first level is deep copy , The second level starts with shallow copies .
2、Object.assign()
MDN That's what it says on the paper :
Object.assign()Method is used to assign the values of all enumerable properties from one or more source objects to the target objects . It will return the target object .
Used for object merging , It is written as follows :
let obj = Object.assign({}, obj1, obj2);
console.log(obj); // { name: ' Chen Weiting ', gender: ' male ', hobby: ' dance ', nationality: ' China ' }As can be seen from the above code , The effect is the same as method 1 .
notes : The first level is deep copy , The second level starts with shallow copies .
3、 Recursive assignment
let obj = obj1;
for (var p in obj2){
if(obj2.hasOwnProperty(p))
obj[p] = obj2[p];
}
console.log(obj); // { name: ' Chen Weiting ', gender: ' male ', hobby: ' dance ', nationality: ' China ' }As can be seen from the above code , The effect is the same as method 1 . It is similar to adding attributes by direct assignment
notes : The first level is deep copy , The second level starts with shallow copies
4、jquery Medium extend()
jQuery.extend() Function is used to merge the contents of one or more objects into the target object .
$.extend(obj1, obj2) // Shallow copy
$.extend(true, obj1, obj2) // Deep copy Two 、 Array merge
1、 Extension operators
Use ES6 Syntax extension operator : This method also creates a new array
var newArray = [...array,...elements]
console.log(newArray); // ["a", "b", 0, 1, 2]2、 Use array.concat() Method to merge
Use concat Method : This method is not added to an existing array , Instead, create and return a new array .
var array = ["a", "b"];
var elements = [0, 1, 2];
var newArray = array.concat(elements);
console.log(array); //['a', 'b']
console.log(newArray);// ["a", "b", 0, 1, 2]3、 About Apply
Use Apply Method : This method is to add the array items to another array , Is a way to change the original array
var array = ["a", "b"];
var elements = [0, 1, 2];
array.push.apply(array, elements);
console.log(array); // ["a", "b", 0, 1, 2]4、array.push() Method to merge
const heroes = ['Batman'];
heroes.push('Superman');
heroes; // ['Batman', 'Superman']
const heroes = ['Batman', 'Superman'];
const villains = ['Joker', 'Bane'];
heroes.push(...villains);
heroes; // ['Batman', 'Superman', 'Joker', 'Bane']
边栏推荐
- Uniapp develops wechat official account, and the drop-down box selects the first one in the list by default
- 2.登陆退出功能开发
- 抓包工具charles實踐分享
- 283.移动零
- 【IEEE出版】2022年智能交通与未来出行国际会议(CSTFM 2022)
- [IEEE publication] 2022 International Conference on service robots (iwosr 2022)
- Leetcode-1051: height checker
- [IEEE publication] 2022 International Conference on industrial automation, robotics and Control Engineering (iarce 2022)
- 2. login and exit function development
- [resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)
猜你喜欢

Sort out interface performance optimization skills and kill slow code

【Energy Reports期刊发表】2022年能源与环境工程国际会议(CFEEE 2022)

线程池的执行流程

26.删除有序数组的重复项

Flink集群搭建以及企业级yarn集群搭建

JMeter interface test tool foundation - sampler (II)

Appium automation test foundation - mobile end test environment construction (I)

23. Opencv——图像拼接项目

机械臂速成小指南(一):机械臂发展概况

leetCode-498: 对角线遍历
随机推荐
126. 单词接龙 II BFS
6.套餐管理业务开发
Flink cluster construction and enterprise level yarn cluster construction
跨域概述,简单积累
Quick completion guide for mechanical arm (zero): main contents and analysis methods of the guide
Safety and food security for teachers and students of the trapped Yingxi middle school
程序员在技术之外,还要掌握一个技能——自我营销能力
Outils de capture de paquets
2022年智能机器人与系统国际研讨会(ISoIRS 2022)
Spark submission parameter -- use of files
Cross domain overview, simple accumulation
抓包工具charles实践分享
解决DBeaver SQL Client 连接phoenix查询超时
2.登陆退出功能开发
Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars
Practice sharing of packet capturing tool Charles
The difference between static link library and dynamic link library
23. Opencv——图像拼接项目
Niuke-top101-bm28
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)