当前位置:网站首页>JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method
JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method
2022-07-28 01:57:00 【No lazy duck】
Let's start with one js Concept —>
Object serialization : Is to change the state of an object into a string (JSON.stringify), You can also restore strings to objects (JSON.parse).
We usually use this method to Copy one js object , But in some cases , There will be problems. , For example, in the following cases .
Among objects NaN、Infinity and -Infinity When , After serialization, it will become null.
Among objects undefined and Function When it comes to type data , After serialization, it will be lost directly .
When there is a time type in the object , After serialization, it becomes a string type .
When an object is referenced circularly , Errors will be reported directly after serialization .
const obj = {
nan:NaN,
infinityMax:1.7976931348623157E+10308,
infinityMin:-1.7976931348623157E+10308,
undef: undefined,
fun: () => {
console.log(123) },
date:new Date,
name:' Zhang San ',
wife:obj.name
}
It is recommended to use recursive method to encapsulate a tool function in the project
//utils.js
//...
const deepCopy= (obj = {
}) => {
// Empty variable first
let newobj = null;
// Determine whether recursion needs to continue
if (typeof (obj) == 'object' && obj !== null) {
newobj = obj instanceof Array ? [] : {
};
// Perform the next level of recursive cloning
for (var i in obj) {
newobj[i] = deepCopy(obj[i])
}
// If the object is not directly assigned
} else newobj = obj;
console.log(newobj)
return newobj;
}
Use :
let person= {
name:'zhangsan',
age:26,
wife:{
sheName:'xiaohua',
sheAge:25
}
}
let newPerson= deepCopy(person)

边栏推荐
- HCIP第十三天笔记
- GBase 8c 事务ID和快照(一)
- Unity 通用红点系统
- 数商云供应链集采管理系统解决方案:集采系统管理模式,数字化管控企业物资
- 以“数字化渠道”撬动家用电器消费蓝海,经销商在线系统让企业生意更进一步
- How tormenting are weekly and monthly reports? Universal report template recommended collection! (template attached)
- GBase 8c 事务ID和快照
- The story of amen
- Tencent cloud hiflow scene connector
- Gbase 8C transaction ID and snapshot
猜你喜欢

Qlib tutorial - based on source code (II) local data saving and loading

周报、月报有多折磨人?万能报表模板建议收藏!(附模板)

HCIP第十二天笔记

石油化工行业迎战涨价大潮,经销商分销系统平台数字化赋能经销商与门店

网易云仿写

FreeRTOS内核小结

存储成本降低 80%,有赞数据中台成本治理怎么做的?

26.抽象化和模板思想

Zhi Huijun, Huawei's "genius youth", has made a new work, building a "customized" smart keyboard from scratch

Establishment of elk log analysis system
随机推荐
GBase 8c 恢复控制函数
GBase 8c 数据库对象尺寸函数(一)
LeetCode 第 302 场周赛
Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice
石油化工行业迎战涨价大潮,经销商分销系统平台数字化赋能经销商与门店
Gbase 8C transaction ID and snapshot (IV)
2022软件测试技能 Robotframework + SeleniumLibrary + Jenkins web关键字驱动自动化实战教程
VPP之DPDK插件
Redis 5 种基本数据结构
Gbase 8C transaction ID and snapshot
白质脑功能网络图论分析:抑郁症分类和预测的神经标记
HCIP第十二天笔记
MPLS 隧道实验
嵌入式经典通信协议
【taichi】在太极中画出规整的网格
Gbase 8C configuration setting function
Qlib tutorial - based on source code (II) local data saving and loading
Leetcode: 515. Find the maximum value in each tree row
忘记root密码
[interview: concurrent article 28:volatile] orderliness