当前位置:网站首页>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
边栏推荐
- Visio annotation, annotation location
- MySQL -connector/j driver download
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
- Maxcompute string replacement function -replace
- Workflow module jar package startup error: liquibase – waiting for changelog lock
- Adobe Premiere基礎-聲音調整(音量矯正,降噪,電話音,音高換擋器,參數均衡器)(十八)
- 源码安装MAVROS
- Bloom filter:
- Proxmox VE Install 7.2
- Niuke Xiaobai monthly race 52 E group logarithmic sum (inclusion exclusion theorem + dichotomy)
猜你喜欢
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
DevCloud加持下的青软,让教育“智”上云端
jdbc_ Related codes
Adobe Premiere基础-常用的视频特效(裁剪,黑白,剪辑速度,镜像,镜头光晕)(十五)
mysql -connector/j驱动下载
Prevent form resubmission based on annotations and interceptors
Adobe Premiere foundation - opacity (matte) (11)
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)
Error building SqlSession问题
[target tracking] |stark configuration win OTB
随机推荐
【TcaplusDB知识库】TcaplusDB系统用户组介绍
Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
Two controller layer interface authentication methods
C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
Yolov6+tensorrt+onnx: deployment based on win10+tensorrt8+yolov6+onnx
NVIDIA installs the latest graphics card driver
Longest XOR path (dfs+01trie)
Wechat applet development reserve knowledge
Find the maximum XOR value in the sequence given a number (01 Dictionary)
You can do sideline work
Adobe Premiere foundation - sound adjustment (volume correction, noise reduction, telephone tone, pitch shifter, parameter equalizer) (XVIII)
codeforces每日5题(均1700)-第二天
Distributed | several steps of rapid read / write separation
国内酒店交易DDD应用与实践——理论篇
Serial port experiment based on stm32f103zet6 library function
How to use idea?
lodash深拷贝使用
图像特征计算与表示——基于内容的图像检索
Travel card "star picking" hot search first! Stimulate the search volume of tourism products to rise