当前位置:网站首页>JS deep copy - Notes
JS deep copy - Notes
2022-07-29 05:30:00 【Blue Mountains and waters】
Recursive implementation
Applicable to any data type
// Achieve deep cloning
let ooo={name:'name',age:'age',sex:[['sex']],ok:()=>console.log(1)}
const deepClone=(obj)=>{
// Judge whether it is Object,Array The type of
if(obj&&typeof obj==='object'){
// Reference type
// Judgment is [] still {}
let temp=Array.isArray(obj)?[]:{}
// Cyclic operation , recursive
for(let i in obj){
temp[i]=deepClone(obj[i])
}
// return temp
return temp
}else{
// except Object,Array Types other than return directly obj
return obj
}
}
let obj=deepClone(ooo)
obj.gender='man'
console.log(obj,ooo)
JSON
Data loss ( Such as function )
let obj={name:' Little bastard ',msg:{age:18,gender:'man'}}
let newObj=JSON.parse(JSON.stringify(obj))
newObj.msg.gender=' male '
console.log(obj)
边栏推荐
- AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
- R & D efficiency | analysis of kubernetes' core technology and Devops' landing experience
- 预约中,2022京东云产业融合新品发布会线上开启
- Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
- Custom QML control: imagebutton
- 365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
- QML type: state state
- 水一篇图的拓扑排序
- [event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
- 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
猜你喜欢

京东云金秋上云特惠进行中!扫码参与活动

Day 2

Day 1

【C语言系列】— 打印100~200之间的素数

三次握手四次挥手针对面试总结

力扣994:腐烂的橘子(BFS)

阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂

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

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

数据库操作 Day 6
随机推荐
平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道
浅谈Servlet
【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
直播预告|如何通过“智能边缘安全”提升企业免疫力?
Occt learning 002 - environment construction
Helm chart for Kubernetes
AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
One dimensional array exercise
Introduction to array learning simple question sum of two numbers
Teardown's method of lifting the time limit
第一周总结
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
副作用和序列点
365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
CSDN的md编辑器如何输入上下标?公式和非公式的输入方式不一样
QML control: combobox
What is_ GLIBCXX_ VISIBILITY(default)
千人规模互联网公司研发效能成功之路
Integer overflow and printing