Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The method by which the JS loops through the JSON data


May 08, 2021 JSON


Table of contents


JS loops through JSON data

This article provides examples of JS loop JSON data columns, as well as JS loops traversing JSON data for learning.

1. Column loop of json data

var len = json.length;
for (var i = 0; i < len; i++)
{
for (obj in json[i])
{
var obj2 = obj;
}
}

2.JS loop through the JSON data

JSON data such as:
"Options": "text/":/"Wangjiawan/""value/"
e/:/"10/",""text":/"Yujiawan/", /"value/":/"13"


The method of traversing with the js loop is as follows:

//方法1,
var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name:"d",age:14}]; 
      for(var o in data){ 
        alert(o); 
        alert(data[o]); 
        alert("text:"+data[o].name+" value:"+data[o].age ); 
      } 
//方法2,
<script type="text/javascript"> 
function text(){ 
  var json = {"options":"[{/"text/":/"王家湾/",/"value/":/"9/"},{/"text/":/"李家湾/",/"value/":/"10/"},{/"text/":/"邵家湾/",/"value/":/"13/"}]"}  
  json = eval(json.options) 
  for(var i=0; i<json.length; i++) 
  { 
     alert(json[i].text+" " + json[i].value) 
  } 
} 
</script>