当前位置:网站首页>Stepping on the pit: json Parse and json stringify
Stepping on the pit: json Parse and json stringify
2022-06-29 18:18:00 【Do you eat bananas?】
Catalog
Two 、 Time object Serialized as character string
3、 ... and 、NaN、Infinity and -Infinity Serialized as null
5、 ... and 、RegExp、Error object , Serialized as {}
6、 ... and 、 Circular quotation will report an error
7、 ... and 、 Third party Library ( recommend lodash)
8、 ... and 、 Make a deep copy of yourself
Preface :
In the project, many people try to be simple 、 It is convenient to use... In deep copy of data JSON.parse(JSON.stringify(...)) The way , Little imagine , The deep cloning method of these two combinations has many disadvantages , Use with caution !
One 、 Data loss
When there is... In the serialized data Function or undefined when , After serialization ,Function and undefined Will lose
<script>
let obj = {
name: 'lili',
age: 13,
address: undefined,
sayHello: function() {
console.log(`my name is ${this.name}`);
}
}
let copyObj = JSON.parse(JSON.stringify(obj));
console.log(copyObj);
</script>
Two 、 Time object Serialized as character string
<script>
let obj = {
time1: new Date(),
time2: new Date(1656313196128)
}
console.log(obj);
let copyObj = JSON.parse(JSON.stringify(obj))
console.log(copyObj);
</script>
3、 ... and 、NaN、Infinity and -Infinity Serialized as null
<script>
let obj = {
notANumber: NaN,
positiveNum: Infinity,
minusNum: -Infinity
}
console.log(obj);
let copyObj = JSON.parse(JSON.stringify(obj))
console.log(copyObj);
</script>
Four 、 Only enumerable objects can be serialized own property , If you pass the constructor new The examples come out , You'll lose constructor Constructors
<script>
function Student(name) {
this.name = name
}
let lili = new Student('lili')
let Student1 = {
name: lili,
hobby: ' badminton '
}
console.log(Student1);
let copyStudent1 = JSON.parse(JSON.stringify(Student1))
console.log(copyStudent1);
</script>
5、 ... and 、RegExp、Error object , Serialized as {}
<script>
let obj = {
regularObj: new RegExp('/^1[3456789]\d{9}$/'),
errorObj: new Error(' Something went wrong !')
}
console.log(obj);
let copyObj = JSON.parse(JSON.stringify(obj))
console.log(copyObj);
</script>

6、 ... and 、 Circular quotation will report an error
<script>
let obj = {
name: 'lili'
}
obj.details = obj
console.log(obj);
let copyObj = JSON.parse(JSON.stringify(obj))
console.log(copyObj);
</script>
7、 ... and 、 Third party Library ( recommend lodash)
lodash In addition to deep cloning , There are many practical ways , Details can be found in the official documents :
Lodash brief introduction | Lodash Chinese document | Lodash Chinese net
8、 ... and 、 Make a deep copy of yourself
边栏推荐
- Adobe Premiere foundation - opacity (mixed mode) (XII)
- 程序员资源推荐指南
- Sister Juan takes you to learn database -- 5-day sprint Day1
- jdbc认识上手
- JDBC Codes connexes
- C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
- It's really easy to make money in foreign lead and build a website
- 记录服务器被入侵病毒:ssh密码被更改登录失败、恶意程序跑满了cpu、jar包启动失败自动kill、一直弹出You have new mail in /var/spool/mail/root
- Adobe Premiere Basics - common video effects (corner positioning, mosaic, blur, sharpen, handwriting tools, effect control hierarchy) (16)
- jdbc_相关代码
猜你喜欢

C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad

Premature end of script headers 或 End of script output before headers

【目标跟踪】|stark配置 win otb

How QQ opens online customer service

Error building sqlsession problem

Adobe Premiere foundation - cool text flash (14)

Servlet student management system (Mengxin hands-on version)

Adobe Premiere基础-素材嵌套(制作抖音结尾头像动画)(九)

Adobe Premiere Basics - general operations for editing material files (offline files, replacing materials, material labels and grouping, material enabling, convenient adjustment of opacity, project pa

Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
随机推荐
MySQL - clear data in the table
Proxmox VE Install 7.2
3h精通OpenCV(六)-图像堆叠
Longest XOR path (dfs+01trie)
图像特征计算与表示——基于内容的图像检索
idea怎么使用?
Parental delegation mechanism
回文子串的最大长度(字符串哈希+二分)
/usr/bin/ld: warning: **libmysqlclient. so. 20**, needed by //usr/
markdown知识轻轻来袭
lodash深拷贝使用
Distributed | several steps of rapid read / write separation
Adobe Premiere基础-常用的视频特效(裁剪,黑白,剪辑速度,镜像,镜头光晕)(十五)
Automatic software test - read SMS verification code using SMS transponder and selenium
Sister Juan takes you to learn database -- 5-day dash day4
jdbc_ Related codes
Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
Mysql database daily backup and scheduled cleanup script
Adobe Premiere foundation - batch material import sequence - variable speed and rewind (recall) - continuous action shot switching - subtitle requirements (13)
WBF: new method of NMS post filter frame for detection task?