当前位置:网站首页>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
边栏推荐
- 【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
- Abc253 D fizzbuzz sum hard (tolerance exclusion theorem)
- Visio annotation, annotation location
- Kubekey2.2.1 kubernetes1.23.7 offline package production +harbor Department summer and upload image
- 【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
- C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
- How QQ opens online customer service
- Premature end of script headers 或 End of script output before headers
- shell教程之循环语句for,while,until用法
- When easycvr deploys a server cluster, what is the reason why one is online and the other is offline?
猜你喜欢

kubekey2.2.1 kubernetes1.23.7离线包制作+harbor部暑并上传镜像

Abc253 D fizzbuzz sum hard (tolerance exclusion theorem)

ISO 32000-2 international standard 7.7

jdbc_ Related codes

jdbc_相關代碼

Application and practice of DDD in domestic hotel transaction -- Theory

Parental delegation mechanism

Two controller layer interface authentication methods

Automatic software test - read SMS verification code using SMS transponder and selenium

让 Google 搜索到自己的博客
随机推荐
Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)
字典树(随学)
Adobe Premiere foundation - time remapping (10)
3H proficient in opencv (IX) - the simplest face detection
【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
Distributed | several steps of rapid read / write separation
SSH protocol learning notes
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
jdbc_相关代码
DevCloud加持下的青软,让教育“智”上云端
kubekey2.2.1 kubernetes1.23.7离线包制作+harbor部暑并上传镜像
If the evaluation conclusion of waiting insurance is poor, does it mean that waiting insurance has been done in vain?
Maximum length of palindrome substring (string hash + binary)
markdown知识轻轻来袭
Error building SqlSession问题
【目标跟踪】|stark配置 win otb
Mac installation php7.2
Let Google search your blog
Maxcompute string replacement function -replace
The soft youth under the blessing of devcloud makes education "smart" in the cloud