当前位置:网站首页>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']
边栏推荐
- Wechat applet rich text picture width height adaptive method introduction (rich text)
- Practice sharing of packet capturing tool Charles
- 【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)
- 顺丰科技智慧物流校园技术挑战赛(2022/06/19)【AK】
- leetCode-498: 對角線遍曆
- How to use multiple kindeditor editors on a page and pass values to the server
- Record the range of data that MySQL update will lock
- Distribute proofs of manuscripts by scanning
- 【IEEE出版】2022年工业自动化,机器人与控制工程国际会议(IARCE 2022)
- charles抓包工具使用教程
猜你喜欢

Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars

线程池的状态

24. 图像拼接大作业

Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves

Flink checkpoint and savepoint

抓包工具charles实践分享

leetCode-498: 對角線遍曆

消息队列的作用

抓包工具charles實踐分享

【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)
随机推荐
leetCode-929: 独特的电子邮件地址
线程池的执行流程
Leetcode-1051: height checker
线程调度的常用方法
JMeter interface test tool foundation - badboy tool
1.项目环境搭建
顺丰科技智慧物流校园技术挑战赛(2022/06/19)【AK】
Resolved: methods with the same name as their class will not be constructors in
线程运行原理
Status of the thread pool
5.菜品管理业务开发
分布式系统你必须了解的点-CAP
【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
leetCode-1823: 找出游戏的获胜者
Common methods of thread scheduling
包装类型的缓存机制
Thread pool execution process
26.删除有序数组的重复项
分布式事务原理以及解决分布式事务方案