当前位置:网站首页>深拷贝函数
深拷贝函数
2022-07-20 20:25:00 【@海海人生】
方法一 JSON方法
const objCopy = JSON.parse(JSON.stringify(obj));
该方法函数不能拷贝
方法二 递归
// source 拷贝对象
function deepClone(source) {
// .constructor指向该对象的构造函数
const targetObj = source.constructor === Arry? [] : {
};
// 遍历对象或数组
for(let key in source) {
// obj.hasOwnProperty() 指示对象自身属性中是否具有指定的属性
if(source.hasOwnProperty(key)) {
// 判断自身属性是否存在,且是否为对象,是对象进一步遍历,非对象直接赋值,不存在属性不执行
if(source[key] && typeof(source[key]) ==='object') {
// 维护层代码 进一步判断source[key] 运行代码可去除
targetObj[key] = source[key].constructor === Arry? []: {
}
// 递归
targetObj[key] = deepClone(source[key])
}else{
// source[key]不是对象时直接赋值
targetObj[key]= source[key]
}
}
}
return targetObj
}
边栏推荐
- Awk statistical difference record
- Asemi rectifier bridge mb10m parameters, mb10m size, mb10m characteristics
- Recommendation Book Evolutionary Psychology: the mystery of magic behind Liu Genghong and Wang Xinling
- [error analysis] grid access
- Sword finger offer 34 A path with a certain value in a binary tree
- 剑指 Offer 13. 机器人的运动范围
- 【C语言】文件相关操作
- DS(Tree)
- npm与包
- Production and use of static library
猜你喜欢
SAP 电商云 Spartacus UI Store 相关的设计明细
[200 opencv routines] 235 Principal component analysis for feature extraction (sklearn)
PyCharm 输入光标突然变粗了
受众分析与卸载分析全面升级,HMS Core分析服务6.6.0版本上新
剑指 Offer 58 - I. 翻转单词顺序,strip()函数
Recommendation Book Evolutionary Psychology: the mystery of magic behind Liu Genghong and Wang Xinling
Container network: free days, don't buy an apartment and rent it together
读书会 | 拨开心理咨询的迷雾:普通人应该如何看待心理咨询
[错题]对角线
招募 | “壹脑云科研圈“招募新成员啦
随机推荐
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-2 personal solutions
Asemi rectifier bridge mb10m parameters, mb10m size, mb10m characteristics
根IT创业者的几个绊脚石
Vectorization support and column index of spark parquet nested types
读书笔记(一)——《追风筝的人》
《UAV Intelligent Coverage Navigation Based on DRL in Complex Geometrical Environments》翻译
QT openGL环境光照
【硬件基础4】二极管(原理、特性、类型、电路分析)
金仓数据库KingbaseES数据库管理员指南--15.4. 视图、同义词和序列的数据字典视图
IDEA 2021 自动生成serialVersionUID
金仓数据库KingbaseES数据库管理员指南--15.2. 管理序列
高等数学(第七版)同济大学 习题3-1 个人解答
剑指 Offer 34. 二叉树中和为某一值的路径
Inefficient? Slow response? Pain points of reporting tools and their solutions
全棧開發實戰 | SSM框架整合完整教程
Sword finger offer 25 Merge two sorted linked lists
Hutoo --- 日期时间工具-DateUtil
internship:熟悉项目代码的几个步骤
王炸动力 创富快人一步!祥菱大熊猫2.0动力产品正式上市
科研 | 研究成果该如何署名?