Jquery traversal json and array method summary each ()
In jquery, the most commonly used method for Traversing arrays or json arrays is the each function or the foreach function. for can also be implemented, next I will introduce in detail jquery's implementation of traversing json and arrays. The following code copies the Code. For details, refer to the each () method.
In jquery, the most commonly used method for Traversing arrays or json arrays is the each function or the foreach function. for can also be implemented, next I will introduce in detail jquery's implementation of traversing json and arrays.
First, let's refer to the each () method. each () specifies the function to be run for each matching element. If false is returned, it can be used to stop the loop early.
Syntax.
$ (Selector). each (function (index, element ))
Example:
Each processes one-dimensional arrays
Var arr1 = ["aaa", "bbb", "ccc"];
$. Each (arr1, function (I, val ){
Alert (I );
Alert (val );
});
Alert (I) will output 0, 1, 2
Alert (val) will output aaa, bbb, ccc
Each processes two-dimensional arrays
Var arr2 = [['A', 'aaa', 'aaa'], ['B', 'bbb', 'bbb'], ['C ', 'cc', 'ccc ']
$. Each (arr2, function (I, item ){
Alert (I );
Alert (item );
});
Arr2 is a two-dimensional array, and item is equivalent to taking every array in the two-dimensional array.
Item [0] is relative to the first value in each one-dimensional array.
Alert (I) will output 0, 1, 2, because the two-dimensional array contains three array elements
Alert (item) is output as ['A', 'A', 'aaa'], ['B', 'bb ', 'bbb'], ['C ', 'cc', 'ccc ']
Traverse json data
[
{
"Text": "Wang jiawan ",
"Value": "9"
},
{
"Text": "Li jiawan ",
"Value": "10"
},
{
"Text": "Shao jiawan ",
"Value": "13"
}
]
Alert (data [0] ["text"]); // Shao jiawan
<Script type = "text/javascript" src = "script/jquery-1.2.6.min.js"> </script> <script type = "text/javascript"> $ (document ). ready (function () {$ ("# letter-e. button "). click (function () {$. getJSON (data, function (data) {$ ("# dictionary "). empty (); $. each (data, function (entryIndex, entry) {var html = ''; html + ='' + entry ['text'] + ''; html + = ''+ entry ['value'] +''; html + = ''; $ ('# dictionary '). append (html) ;}) ;};}); </script> a complete test instance of ELoad. For details, refer
$ (Function () {/* common sample method, which can be used for sample object and array. Unlike the $ (). each () method of the jQuery object, this method can be used to sample any object. The callback function has two parameters: the first is the object member or the index of the array, and the second is the corresponding variable or content. to exit the each loop, the callback function returns false, other return values are ignored. * // * Example array, using both the element index and content. $. Each ([0, 1, 2], function (index, content) {alert ("Item #" + index + "its value is:" + content );}); var testPatterns = ['yyyy', 'yy', 'mmmm', 'mmm', 'mm', 'M', 'dd', 'eee ', 'Eee ', 'A', 'hh', 'h', 'hh ', 'h', 'mm', 'M', 'ss','s ', 'S ', 'eeee MMMM, d yyyy hh: mm: ss. S A','m/d/yy HH: mm']; $. each (testPatterns, function () {document. write ('
'+ This +'
') ;}); * // * Traverses the object and uses both the member name and variable content. $. Each ({name: "John", lang: "JS"}, function (index, content) {// alert ("Object Name:" + index + ", and Its Value is: "+ content); alert (" Object Property Name Is: "+ index +", And Its Property Value is: "+ content );}); * // * example object array, using the member variable content at the same time. Var arr = [{name: "John", lang: "JS" },{ name: "Nailwl", lang: "Jquery" },{ name: "Wu lei ", lang: "Ext"}]; $. each (arr, function (index, content) {alert ("The Man's No. is: "+ index +", And "+ content. name + "is learning" + content. lang);}); */}); // -->