当前位置:网站首页>JS, one of the methods of object merging Assign (), recursive assignment & method of array merging..., array. Concat (), array. Push. Apply (), array. Push ()
JS, one of the methods of object merging Assign (), recursive assignment & method of array merging..., array. Concat (), array. Push. Apply (), array. Push ()
2022-07-27 00:24:00 【viceen】
js in , One of the methods of object merging …、Object.assign()、 Recursive assignment & Method of array merging …、array.concat()、array.push.apply()、array.push()
1、 Object to merge
1.1、 Extension operators (…)
es6 in , Object's extension operator (
...) Used to fetch all traversable properties of parameter object , Copy to current object .
let obj1 = {
name: ' Li Shimin ',
gender: ' male ',
hobby: ' The war '
};
let obj2 = {
name: ' Zhu yuanzhang ',
gender: ' male ',
hobby: ' Separation ',
nationality: ' China '
};
let obj = {
...obj1, ...obj2};
console.log(obj); // { name: ' Zhu yuanzhang ', gender: ' male ', hobby: ' Separation ', 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 .
1.2、Object.assign()
MDN On :
**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: ' Zhu yuanzhang ', gender: ' male ', hobby: ' Separation ', 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 .
1.3、 Recursive assignment
let obj = obj1;
for (var p in obj2){
if(obj2.hasOwnProperty(p))
obj[p] = obj2[p];
}
console.log(obj); // { name: ' Zhu yuanzhang ', gender: ' male ', hobby: ' Separation ', 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
1.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
2、 Array merge
2.1、 Extension operators
Use ES6 Syntax extension operator : This method can also create a new array
let arr = [{
name:' Xiao Ming ',id:1}]
let ary = [{
name:' Xiaohong ',id:2}]
var newArray = [...arr,...ary]
console.log(newArray); // [{name:' Xiao Ming ',id:1},{name:' Xiaohong ',id:2}]
2.2、 Use array.concat() Method
Use concat Method : This method is not added to an existing array , Instead, create and return a new array .
let arr = [{
name:' Xiao Ming ',id:1}]
let ary = [{
name:' Xiaohong ',id:2}]
var newArray = arr.concat(ary);
console.log(arr); // [{name:' Xiao Ming ',id:1}]
console.log(newArray); // [{name:' Xiao Ming ',id:1},{name:' Xiaohong ',id:2}]
2.3、 About Apply
Use Apply Method : This method is to add the array items to another array , It's a kind of Change the method of the original array
let arr = [{
name:' Xiao Ming ',id:1}]
let ary = [{
name:' Xiaohong ',id:2}]
arr.push.apply(arr, ary);
console.log(arr); // [{name:' Xiao Ming ',id:1},{name:' Xiaohong ',id:2}]
2.4、array.push() Method
let arr = [{
name:' Xiao Ming ',id:1}]
let ary = [{
name:' Xiaohong ',id:2}]
arr.push(...ary);
console.log(arr); // [{name:' Xiao Ming ',id:1},{name:' Xiaohong ',id:2}]
1
let heroes = [' Angela '];
heroes.push(' The Monkey King ');
console.log(heroes); // [' Angela ', ' The Monkey King ']
2
let heroes = [' Angela ', ' The Monkey King '];
let villains = [' Liu bang ', ' Cao Cao '];
heroes.push(...villains);
console.log(heroes); // [' Angela ', ' The Monkey King ', ' Liu bang ', ' Cao Cao ']
边栏推荐
- Course notes of Professor Dalin of robotics platform
- C and pointer Chapter 18 runtime environment 18.2 interface between C and assembly language
- 查看 Anaconda 创建环境的位置
- 20220720折腾deeplabcut2
- LeetCode——哈希表篇
- Three tier architecture simulation
- 数据库:MySQL基础+CRUD基本操作
- Double. isNaN(double var)
- 转置卷积相关
- Leetcode topic - binary tree chapter
猜你喜欢

Request attribute in crawler

LeetCode——链表篇

Signal and system impulse response and step response

转置卷积相关

Midge paper reading notes

卷积神经网络——LeNet(pytorch实现)

What scenarios are Tencent cloud lightweight application servers suitable for?

Codeforces C1. Simple Polygon Embedding

Pytorch data pipeline standardized code template

Method of realizing program startup and self startup through registry
随机推荐
Design of alcohol detector based on 51 single chip microcomputer
Dynamic memory management
LeetCode——链表篇
MySQL optimization
Web middleware log analysis script 2.0 (shell script)
Design of vision protector based on 51 single chip microcomputer
Anaconda = > pycharm=> CUDA=> cudnn=> pytorch environment configuration
放图仓库-3(功能图像)
Deployment of yolov5 on Jetson nano deepstream
LeetCode——哈希表篇
Request attribute in crawler
放图仓库-2(函数图像)
C and pointers Chapter 18 runtime environment 18.4 summary
Iptables prevent nmap scanning and binlog
Transpose convolution correlation
Database: MySQL foundation +crud basic operation
Codeforces d.constructing the array (priority queue)
Complete backpack and 01 Backpack
Downloading and processing of sentinel-2
CSDN文章语法规则