Home > Web Front-end > JS Tutorial > body text

A simple implementation method of jQuery using $.each to traverse json array_jquery

WBOY
Release: 2016-05-16 15:05:08
Original
2230 people have browsed it

The example in this article describes the simple implementation method of jQuery using $.each to traverse the json array. Share it with everyone for your reference, the details are as follows:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="content-type" content="text/html; charset=gb2312" />
 <title>jquery $.each遍历json数组方法</title>
 <script type="text/javascript" src="jquery.js"></script>
</head>
<body>
 <script>
 var arr = [{ name: "john", lang: "js" },{ name: "nailwl", lang: "jquery" },{ name: "吴磊", lang: "ext" }]; 
 $.each( arr, function(index, content)
 { 
  alert( "the man's no. is: " + index + ",and " + content.name + " is learning " + content.lang ); 
 });
 </script>
</body>
</html>
 </body>
</html>

Copy after login

$().each() method of jquery object, this method can be used to iterate over any object

The callback function has two parameters:

The first one is the member of the object or the index of the array

Loop through an array, using both element index and content

$.each( [0,1,2], function(index, content){
 alert( "item #" + index + " its value is: " + content );
});

Copy after login

The second one is the corresponding variable or content

If you need to exit the each loop, you can make the callback function return false, and other return values ​​will be ignored.

jquery.each(obj,fn,arg)

This method has three parameters: the object obj to be operated on, the function fn to be operated on, and the function parameters args.

1.obj object is an array

The

each method will call the fn function one by one on the sub-elements in the array until the result returned by calling a certain sub-element is false. In other words, we can process it with the provided fn function to make it meet certain conditions. Just exit the each method call. When the each method provides the arg parameter, the parameter passed in by the fn function call is arg, otherwise: the sub-element index, the sub-element itself

2.obj object is not an array

The biggest difference between this method and 1 is that the fn method will be executed one after another without considering 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 parameters passed in the call are 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;
} 

Copy after login

Readers who are interested in more jQuery-related content can check out the special topics on this site: "A summary of jQuery operation json data techniques", "A summary of jQuery switching special effects and techniques", " Summary of jQuery drag and drop special effects and techniques", "Summary of jQuery extension techniques", "Summary of common classic jQuery special effects", "jQuery animation and special effects Usage summary ", "jquery selector usage summary " and "jQuery common plug-ins and usage summary "

I hope this article will be helpful to everyone in jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!