当前位置:网站首页>Array and object copy of ES6
Array and object copy of ES6
2022-07-21 19:59:00 【OK_ boom】
Just remember es6 Array of 、 Object Copy
let srcArray=[{
key:'value'},'b','c']
- Array copy
let [...cloneArray]=srcArray
perhaps
let cloneArray2=[...srcArray]
Above is a light copy , If the elements of the array are objects , Their elements point to the same object , That is to say, modify it casually srcArray[0].key Will affect cloneArray and cloneArray2 Of [0].key Value — Because they point to the same object .
- use Object.assign Implement deep copy
It can only achieve one layer of depth ! On the basis of the above shallow copy, determine whether each element is an object , If so, create a new object to replace it :
for (var i=0;i<cloneArray.length;i++){
if (typeof cloneArray[i]=='object'){
cloneArray[i]=Object.assign({
},cloneArray[i])
}
}
stay chrome Run the above script on the console ,cloneArray[0] and srcArray[0] It is already two independent objects , change cloneArray[0] The properties of will not affect srcArray[0]
- use JSON Realize real trial copy
let clone2=JSON.parse(JSON.stringify(srcArray))
clone2 It will be a completely new object
边栏推荐
- JSP page
- STM32 - general timer control ultrasonic sensor hcsr04
- 《构建之法》笔记---第六章 敏捷流程
- Unity ECS test demo
- Quanzhi a40i development board hardware specification - 100% domestic + industrial level scheme (Part 2)
- Job regular sed
- Momenta“飞轮式L4”接受夜间长尾场景「像素级」挑战,表现堪比老司机
- ImportError: DLL load failed while importing win32gui: 找不到指定的模块。
- 基于hydra库实现yaml配置文件的读取(支持命令行参数)
- 为什么要写单元测试?如何写单元测试?
猜你喜欢
Metauniverse iwemeta: brain computer interface on the air outlet, secretly decrypting the brain
数据中台、BI业务访谈(二):组织架构梳理的坑
MyCat的介绍与安装以及基本使用
STM32——通用定时器控制超声波传感器HCSR04
How Jenkins sends e-mail? The automation veteran will teach you
(四)事务
Learning notes introduction to explain
(4) Transactions
Quanzhi a40i development board hardware specification - 100% domestic + industrial level scheme (Part 1)
项目管理成熟度模型及项目量化管理
随机推荐
(三)JDBCTemplate
Quanzhi a40i development board hardware specification - 100% domestic + industrial level scheme (Part 1)
Written examination mandatory training day 20
629. K inverse pairs array
Mes réflexions sur les tests automatisés d'assurance - chômage
学习笔记-Explain的介绍
京东爆款架构师成长手册首发,架构师光环你也值得拥有
Unity ECS test demo
es6相关面试题
Content with element type "resultmap" must match "(constructor?, id*, result*, association*, collection*, discriminato?) “
全志A40i开发板硬件说明书——100%国产+工业级方案(下)
iwemeta:史玉柱的黄金酱酒:贴牌的代工厂都被关停了,谁在生产?
C——编译预处理
10行代码实现微信小程序支付功能,使用小程序云开发实现小程序支付功能(含源码
NXP i.MX 8m Mini development board specifications, quad core arm cortex-a53 + arm cortex-m4
cuMemcpyHtoDAsync failed: invalid argument
Ens reading notes
Attack and defense world ----- favorite_ number
input: dynamic input is missing dimensions in profile
接口测试常用工具及测试方法