当前位置:网站首页>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']
边栏推荐
- Six states of threads
- leetCode-1089: 复写零
- 2022 International Symposium on intelligent robots and systems (isoirs 2022)
- Flink checkPoint和SavePoint
- [resource sharing] the 5th International Conference on civil, architectural and environmental engineering in 2022 (iccaee 2022)
- Wechat applet rich text picture width height adaptive method introduction (rich text)
- 栈题目:括号的分数
- Appium自动化测试基础 — 移动端测试环境搭建(一)
- 2022年智能机器人与系统国际研讨会(ISoIRS 2022)
- JMeter interface test tool foundation - sampler (II)
猜你喜欢

希尔排序图文详解+代码实现

Leetcode-2221: triangular sum of arrays

Practice sharing of packet capturing tool Charles

Petit guide de construction rapide du bras mécanique (II): application du bras mécanique

多线程的应用 - 提升效率

Spark submission parameter -- use of files

Quick completion guide for manipulator (III): mechanical structure of manipulator

Quick completion guide for mechanical arm (zero): main contents and analysis methods of the guide

Distributed transaction principle and solution

Flink cluster construction and enterprise level yarn cluster construction
随机推荐
Appium自动化测试基础 — 移动端测试环境搭建(一)
包装类型与基本类型的区别
JMeter interface test tool foundation - use badboy to record JMeter script
Stack Title: fractions in parentheses
Record the range of data that MySQL update will lock
抓包工具charles實踐分享
Process and multithreading
Learn to use PHP to implement unlimited comments and unlimited to secondary comments solutions
2.登陆退出功能开发
Leetcode-1089: replication zero
leetCode-面试题 16.06: 最小差
283. move zero
numpy. linspace()
pycharm快捷键大全
Multithreaded applications - improve efficiency
Normal equation
栈题目:函数的独占时间
Thread operation principle
Quick completion guide for manipulator (III): mechanical structure of manipulator
charles抓包工具使用教程