Jquery traversal json and array method summary each (), jqueryjson

Source: Internet
Author: User

Jquery traversal json and array method summary each (), jqueryjson

 

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

For a complete test example, 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 ('<div>' + this + '</div> <br/>') ;}); * // * 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);}); */}); // --> </mce: script> <title> Jquery each Demo </title> 




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.