当前位置:网站首页>js深拷贝-笔记
js深拷贝-笔记
2022-07-29 05:09:00 【青山绿水的蓝】
递归实现
适用任何数据类型
//实现深克隆
let ooo={name:'name',age:'age',sex:[['sex']],ok:()=>console.log(1)}
const deepClone=(obj)=>{
//判断是否为Object,Array的类型
if(obj&&typeof obj==='object'){
//引用类型
//判断是[]还是{}
let temp=Array.isArray(obj)?[]:{}
//循环操作,递归
for(let i in obj){
temp[i]=deepClone(obj[i])
}
//返回temp
return temp
}else{
//除Object,Array以外的类型直接返回obj
return obj
}
}
let obj=deepClone(ooo)
obj.gender='man'
console.log(obj,ooo)
JSON
会导致数据丢失(如函数)
let obj={name:'小王八',msg:{age:18,gender:'man'}}
let newObj=JSON.parse(JSON.stringify(obj))
newObj.msg.gender='男'
console.log(obj)
边栏推荐
- 预约中,2022京东云产业融合新品发布会线上开启
- 直播预告|如何节省30%人工成本,缩短80%商标办理周期?
- 携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”
- GPIO的输入输出详解
- 365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t
- Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse
- 365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
- C language first level pointer
- PyQt5:第一章第1节:使用Qt组件创建一个用户界面-介绍
- QT learning: qdropevent drag event
猜你喜欢

·来一篇编程之路的自我介绍吧·

牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解

C语言 一级指针

365 day challenge leetcode 1000 questions - day 040 design jump table + avoid flooding + find the latest grouping with size M + color ball with reduced sales value

直播预告|如何通过“智能边缘安全”提升企业免疫力?

PyQt5:第一章第1节:使用Qt组件创建一个用户界面-介绍

研发效能生态完整图谱&DevOps工具选型必看

QML control: combobox

Live broadcast Preview: integration of JD cloud Devops and jfrog product library

科班同学真的了解未来的职业规划吗?
随机推荐
Day 1
Best practices for elastic computing in the game industry
C语言 一维数组
200 多家 ISV 入驻!阿里云计算巢发布一周年
【活动预告】云上数字工厂与中小企业数字化转型创新论坛
京东云分布式链路追踪在金融场景的最佳实践
刷题狂魔—LeetCode之剑指offer58 - II. 左旋转字符串 详解
167. Sum of two numbers II - enter an ordered array
数据泄漏、删除事件频发,企业应如何构建安全防线?
微信小程序视频上传组件直接上传至阿里云OSS
365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing
Allocate memory: malloc() and free()
第一周总结
AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
167. 两数之和 II - 输入有序数组
CMU15-213 Shell Lab实验记录
EXIT中断详解
What is_ GLIBCXX_ VISIBILITY(default)
365天挑战LeetCode1000题——Day 041 二分查找完结纪念 + 第 N 个神奇数字 + 在线选举
冒泡排序 C语言