Home > Article > Web Front-end > A simple implementation method of jQuery using $.each to traverse json array
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>
The $().each() method of the jquery object, this method can be used to loop through any object
The callback function has two parameters:
The first one is the member of the object or the index of the array
Iterates through the 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, 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. The 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 sub-element is false. In other words, we It can be processed by the provided fn function to exit the each method call after meeting certain conditions. When the each method provides the arg parameter, the parameter passed in by the fn function call is arg, otherwise: the subelement index, the subelement itself
2.obj The object is not an array
This method is the same The biggest difference between 1 and 1 is that the fn method will be executed one by one 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 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; }
I hope this article will be helpful to everyone in jQuery programming.
For more jQuery simple implementation methods of using $.each to traverse json arrays, please pay attention to the PHP Chinese website!