当前位置:网站首页>JSON in JS (launch object deep copy)
JSON in JS (launch object deep copy)
2022-07-28 05:16:00 【M78_ Domestic 007】
Full name :JavaScript Object Notation
JSON Is a string , A string format often used to transmit data over the network
Common string formats :
querystring Query string “name=karen&pwd=abc123&count=20"
template Template string `<div styLe="">666</div>`
JSON json character string ‘{"name " : " karen" , "its" : [ "heLLo" , "h5"]}’
JSON analysis
parse() Method Parse the string into objects
Anti parsing stringify() Parse the object into a string
Code display :
<script>
var re="JSON data "
var obj=JSON.parse(re)
console.log(re,obj)
//"JSON data " JSON data
var obj={name:"karen"}
console.log(obj)
//{name:"karen"}
var str=JSON.stringify(obj)
console.log(str)
//{"name":"karen"}
</script>Launch object deep copy 1( If there is no internal reference data or time regularity null Wait for data )
Ask for obj2 Object and the obj={name : "karen"} It looks the same , But they are not the same object , Can't write obj2={name : "karen"}
<script>
var obj={name:"karen"}
// Wrong way :
var obj2={}
obj2.name=obj.name
console.log(obj2,obj==obj2)//true
// It still points to the same memory space
// The right way :
var str=JSON.stringify(obj)//'{"name":"karen"}'
var obj2=JSON.parse(str)//{name:"karen"}
console.log(obj2,obj==obj2)//{name:"karen"} //false
</script>Object deep copy 2
<script>
function deepcopy(data){
if(typeof(data)=="object"){
if(data.constructor==Date){
return new Date(data.getTime())
}
else if(data==null){
return null
}
else if(data.constructor==RegExp){
return new RegExp(data)
}
else if(data.constructor==Array){
var newArray=new Array()
for(var i=0;i<data.length;i++){
var temp=data[i]
newArray.push(arguments.callee(temp))
}
return newArray
}
else{
var newobj=new data.constructor() //new Object()
// Object.keys()
for (var key in data) {
newobj[key]=arguments.callee(data[key])
}
return newobj
}
}
else{
return data
}
}
</script>边栏推荐
- CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
- POJ 1330 Nearest Common Ancestors (lca)
- Internal implementation principle of yymodel
- HashSet add
- MySQL(5)
- Supervisor series: 5. Log
- Pipe /createpipe
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
- How to quickly locate bugs? How to write test cases?
- HDU 1914 the stable marriage problem
猜你喜欢

【ARXIV2203】SepViT: Separable Vision Transformer

The most detailed installation of windows10 virtual machine, install virtual machine by hand, and solve the problem that the Hyper-V option cannot be found in the home version window

FreeRTOS个人笔记-任务通知

Gan: generative advantageous nets -- paper analysis and the mathematical concepts behind it

Reading notes of SMT practical guide 1

mysql的日期与时间函数,varchar与date相互转换

MySQL date and time function, varchar and date are mutually converted

Online sql to XML tool

这种动态规划你见过吗——状态机动态规划之股票问题(中)

Duoyu security browser will improve the security mode and make users browse more safely
随机推荐
HDU 3585 maximum shortest distance
Reading sdwebimage source code Notes
POJ 3728 the merchant (online query + double LCA)
【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
微服务故障模式与构建弹性系统
Look at the experience of n-year software testing summarized by people who came over the test
HDU 1530 maximum clique
FreeRTOS personal notes - task notification
POJ 3417 network (lca+ differential on tree)
Why is MD5 irreversible, but it may also be decrypted by MD5 free decryption website
FPGA:使用PWM波控制LED亮度
list indices must be integers or slices, not tuple
Flink mind map
HDU 3592 World Exhibition (differential constraint)
【SLAM】LVI-SAM解析——综述
Automated test tool playwright (quick start)
Gan: generative advantageous nets -- paper analysis and the mathematical concepts behind it
【ARXIV2205】Inception Transformer
MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?
Paper reading notes -- crop yield prediction using deep neural networks