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>

<body>
<script language= "Web Effects" >
var arr = [{name: "John", Lang: "JS"},{Name: "Nailwl", Lang: "jquery"},{name: "Wu Lei", Lang: "ext"}];
$.each (arr, function (index, content)
{
Alert ("The Man" No.) is: "+ index +", and "+ Content.name +" is learning "+ Content.lang);
});

</script>
</body>
<!--
The $ (). each () method of the JQuery object, which can be used to sample any object
The callback function has two parameters:
The first is the index of the member or array of the object
Examples of arrays, using element indexes and 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 obj to operate, the function FN to operate, and the function's parameter args.

The Obj object is an array
Each method invokes the FN function of the neutron element of the array, until the result returned by a call to a child element is false, that is, we can exit each method call when the supplied FN function is processed so that it satisfies a certain condition. When the arg argument is supplied by the each method, the argument passed in by the FN function is 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 is carried out without regard to the return value. In other words, all the properties of the Obj object are invoked by the FN method, even if the FN returns false. Calling incoming arguments is 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;
}  
;

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.