当前位置:网站首页>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 ']
边栏推荐
- Configure deeplobcut 1 with your head covered
- Go exceed API source code reading (IV) -- save (), SaveAs (name string)
- ResNet论文解读及代码实现(pytorch)
- 机器学习模型——lightGBM
- Deployment of yolov5 on Jetson nano deepstream
- Double. isNaN(double var)
- PTA 7-4 small generation (DFS)
- Lt9611ux Mipi to HDMI 2.0 dual port with audio
- 1、 Kubernetes basic concept + environment installation (build cross server public network environment)
- Codeforces d.constructing the array (priority queue)
猜你喜欢

Dynamic memory management

01 knapsack problem 416. Segmentation and equal sum subset -494. Goal and

蒙着头配置deeplabcut2

The crawler parses the object of the web page. Element name method

Sliding window problem summary

Configure deeplobcut2 with your head covered

Request attribute in crawler

三层架构 模拟

蒙着头配置deeplabcut 1

知识蒸馏——pytorch实现
随机推荐
傅里叶分析(基础介绍)
V-viewer use
4-4 对象生命周期
画冲击函数
PTA 7-3 lists leaf nodes
MySQL optimization
LeetCode——哈希表篇
Lt9611ux Mipi to HDMI 2.0 dual port with audio
Midge paper reading notes
爬虫解析网页的find方法
Alexnet (pytoch Implementation)
13_集成学习和随机森林(Ensemble Learning and Random Forests)
知识蒸馏——pytorch实现
5_线性回归(Linear Regression)
Deeplabcut uses 1
Web middleware log analysis script 2.0 (shell script)
Complex SQL_ 01
The difference between SQL join and related subinquiry
Practice of data storage scheme in distributed system
深度学习调参技巧