当前位置:网站首页>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
边栏推荐
- 让 Google 搜索到自己的博客
- Visio annotation, annotation location
- DevCloud加持下的青软,让教育“智”上云端
- Adobe Premiere foundation - opacity (mixed mode) (XII)
- 3H proficient in opencv (VIII) - shape detection
- MySql存储过程循环的使用分析详解
- 最长异或路径(dfs+01trie)
- 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
- 自动化软件测试 - 利用短信转发器结合Selenium读取短信验证码
- Adobe Premiere foundation - time remapping (10)
猜你喜欢

Adobe Premiere基础-不透明度(混合模式)(十二)

How to use idea?

ISO 32000-2 international standard 7.7

What technology is an applet container? Can it help Internet of things enterprises break through the red sea?

Yolov6+tensorrt+onnx: deployment based on win10+tensorrt8+yolov6+onnx

Two controller layer interface authentication methods

jdbc_相關代碼

Adobe Premiere基础-炫酷文字快闪(十四)

Software testing - you may not understand the basic theoretical knowledge

Configure the local domain name through the hosts file
随机推荐
JS merge two 2D arrays and remove the same items (collation)
Shell tutorial circular statements for, while, until usage
When easycvr deploys a server cluster, what is the reason why one is online and the other is offline?
[tcapulusdb knowledge base] tcapulusdb system user group introduction
Servlet student management system (Mengxin hands-on version)
idea怎么使用?
Distributed | several steps of rapid read / write separation
Goldfish rhca memoirs: do447 building advanced job workflow -- using fact cache to improve performance
Mac installation php7.2
MySql存储过程循环的使用分析详解
Adobe Premiere基础-常用的视频特效(裁剪,黑白,剪辑速度,镜像,镜头光晕)(十五)
Adobe Premiere Foundation - réglage du son (correction du volume, réduction du bruit, tonalité téléphonique, changement de hauteur, égaliseur de paramètres) (XVIII)
Automatic software test - read SMS verification code using SMS transponder and selenium
Jar包后台启动并输出日志
3h精通OpenCV(五)-透视变换
国内酒店交易DDD应用与实践——理论篇
美法官裁定,被控掩盖黑客行为的Uber前安全主管必须面对欺诈指控
金鱼哥RHCA回忆录:DO447构建高级作业工作流--创建作业模板调查以设置工作的变量
Premature end of script headers 或 End of script output before headers
Wechat applet development reserve knowledge