jquery Traversal JSON Array instance method

Source: Internet
Author: User
Tags instance method

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.