当前位置:网站首页>Deep copy function
Deep copy function
2022-07-21 15:00:00 【@Haihai life】
Method 1 JSON Method
const objCopy = JSON.parse(JSON.stringify(obj));
This method function cannot be copied
Method 2 recursive
// source Copy the object
function deepClone(source) {
// .constructor Constructor pointing to the object
const targetObj = source.constructor === Arry? [] : {
};
// Traversing objects or arrays
for(let key in source) {
// obj.hasOwnProperty() Indicates whether the specified property exists in the object's own property
if(source.hasOwnProperty(key)) {
// Judge whether the attribute exists , And whether it is an object , Is the further traversal of the object , Non object direct assignment , There is no attribute and it is not executed
if(source[key] && typeof(source[key]) ==='object') {
// Maintenance layer code Further judgment source[key] Running code can remove
targetObj[key] = source[key].constructor === Arry? []: {
}
// recursive
targetObj[key] = deepClone(source[key])
}else{
// source[key] Direct assignment when not an object
targetObj[key]= source[key]
}
}
}
return targetObj
}
边栏推荐
- TCP sliding window explanation (very practical)
- Hutoo --- 日期时间工具-DateUtil
- 性能提升30倍丨基于 DolphinDB 的 mytt 指标库实现
- 《UAV Intelligent Coverage Navigation Based on DRL in Complex Geometrical Environments》翻译
- 金仓数据库KingbaseES数据库管理员指南--17数据库调度概念
- 云原生如何支撑企业 IT 治理 | 阿里云用户组
- CONDA upgrade tensorflow gpu=2.5.0 and cudn, cudatoolkit version
- SQL statement
- When you delete it, you delete it. This kind of capacitance should not have appeared
- 阿里云原生应用平台架构师田伟:应用架构的规划、治理与演进
猜你喜欢
[wrong question] diagonal
[applet] come and develop your first wechat game (detailed process)
Win64 驱动内核编程-31.枚举与删除映像回调
Win64 驱动内核编程-30.枚举与删除线程回调
【小程序】快来开发你的第一个微信小游戏(详细流程)
乘数科技云管控平台适配阿里云 PolarDB,共促云原生数据库生态繁荣
算术运算符2(阁瑞钛伦特软件-九耶实训)
6.栈、栈帧
Web3流量聚合平台Starfish OS,给玩家元宇宙新范式体验
Win64 driver kernel programming -31 Enumerating and deleting image callbacks
随机推荐
Vectorization support and column index of spark parquet nested types
[错题]对角线
Do you really know the influence of AC coupling capacitance?
【Kaggle】如何有效避免OOM(out of memory)和漫长的炼丹过程
欧菲光:已布局运动相机、智能家居等光学镜头 / 影像模组,部分产品已实现量产
【C 练习】将字符串中的空格转换为「%20」
乘数科技云管控平台适配阿里云 PolarDB,共促云原生数据库生态繁荣
Supermarket order management system based on SSM framework +mysql [source code + document +ppt]
Pycharm input cursor suddenly thickens
【无标题】
Yunna Xianning communication machine room dynamic loop monitoring system, telecom dynamic loop monitoring system
Win64 驱动内核编程-32.枚举与删除注册表回调
[JS] console command you don't know
【LeetCode】29、 两数相除
LoRa基站覆盖范围
Win64 driver kernel programming -31 Enumerating and deleting image callbacks
[quick start tutorial 3] crazy shell · open source formation UAV - development environment construction
Methods for JS object to obtain attributes (. And [] methods)
Audience analysis and uninstall analysis have been comprehensively upgraded, and HMS core analysis service version 6.6.0 has been updated
Win64 驱动内核编程-30.枚举与删除线程回调