We know that traversing an ordinary array can be done using each, as follows
The code is as follows |
Copy Code |
var _mozi=[' mohism ', ' Mo-tse ', ' Modi ', ' love not attack ', ' still with Shangxian ']; The array used in this article is the same as $.each (_mozi,function (key,val) { The callback function has two parameters, the first is the element index, and the second is the current value Alert (' _mozi array, index: ' +key+ ' corresponds to the value: ' +val '); }); |
Of course, we can also use for. In,each a little stronger. For.. In can also traverse the array and return the corresponding index, but the value needs to be obtained through Arrname[key].
JS Traversal JSON array
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var json = {"Options": "[{" Text ":" Wangjiawan "," Value ":" 9 "},{" text ":" Li Jia Wan "," value ":" Ten "},{" text ":" Shaojia Bay "," Value ":" 13 "}]"} JSON = eval (json.options) for (var i=0; i<json.length; i++) { Alert (json[i].text+ "" + json[i].value) } </script>
|
So how does jquery do that?
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var d1 =[{"text": "Wangjiawan", "Value": "9"},{"text": "Li Jia Wan", "value": "},{" "Text": "Shaojia Bay", "Value": "13"}]; $ (D1). each (function () { Alert (this.text+ "" +this.value); }); </script> |
Easy, it's done.