Example of deep assignment to a json array in javascript

  • 2020-03-30 03:35:19
  • OfStack

<!DOCTYPE HTML> 
<html> 
<head> 
<title>test1</title> 
</head> 
<body> 
<script> 
var arr={ 
"name":"zhansan", 
"age":23, 
"address":{ 
"city":"beijing", 
"gas":{ 
"gasSheet":" Gaseous efflux monthly report ", 
"H_adjust":1 
}, 
"time":{ 
"duration":31 
} 
}, 
"units":{"title":"function"} 
} 
function show(obj){ 
var tmp={}; 
for(var i in obj){ 
if(obj[i]=="object"){ 
<span style="color:#ff0000;">tmp[i]=show(obj[i]);</span> 
} 
else{ 
<span style="color:#ff0000;">tmp[i]=obj[i]</span> 
} 
} 
return tmp; 
} 

var result=show(arr); 
console.log(result); 

</script> 
</body> 
</html>

</pre><p></p><p> Use the idea of recursion, where attention is paid to assignment statements </p><p> Can be used in different environments </p><p></p><pre>

Related articles: