jquery $.each Traversal JSON array method

Source: Internet
Author: User

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>jquery $.each Traversal JSON array method </title>
<script type= "Text/javascript" src= "Jquery.js" ></script>
<body>
<script>
var arr = [{name: "John", Lang: "JS"},{Name: "Nailwl", Lang: "jquery"},{Name: "Shaman", Lang: "ext"}];
$.each (arr, function (index, content)
{
Alert ("The man ' s No.") is: "+ index +", and "+ Content.name +" is learning "+ Content.lang);
});

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

<!--
The $ (). each () method of the JQuery object, which can be used to sample any object
A callback function has two parameters:
The first is the index of a member or array of objects
Example of an array, using both the element index and the content
$.each ([0,1,2], function (index, content) {
Alert ("Item #" + Index + "Its value is:" + content);
});

The second one is the corresponding variable or content
If you need to exit each loop so that the callback function returns false, the other return values are ignored.


Jquery.each (Obj,fn,arg)
The method has three parameters: the object to be manipulated by obj, the function of the FN, the function of the parameter args.

The Obj object is an array
Each method makes an FN function call to each of the child elements of the array, until the call to a sub-element returns False, that is, we can exit each method call after the supplied FN function is processed so that it satisfies certain conditions. When the each method provides the arg parameter, the FN function call passes in the argument arg, otherwise: the child element Index, the child element itself
2.obj object is not an array
The biggest difference between this method and 1 is that the FN method will be carried out regardless of the return value. In other words, all properties of the Obj object will be called by the FN method, even if the FN function returns false. The call passed in with a parameter similar to 1.
Jquery.each=function (obj, FN, args) {
if (args) {
if (obj.length = = undefined) {
for (var i in obj)
Fn.apply (obj, args);
}else{
for (var i = 0, ol = obj.length; i < ol; i++) {
if (fn.apply (obj, args) = = = False)
Break

}

}
} else {
if (obj.length = = undefined) {
for (var i in obj)
Fn.call (obj, I, obj);
}else{
for (var i = 0, ol = obj.length, val = obj[0]; I < ol && Fn.call (val,i,val)!== false; val = Obj[++i]) {}

}
}
return obj;
}
-

jquery $.each Traversal JSON array method

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.