JQuery $. each traversal object and array usage instance _ jquery

Source: Internet
Author: User
This article mainly introduces jQuery $. examples of using each to traverse objects and arrays. This article describes examples of traversing objects and traversing arrays with and without parameters and several common usage of the each method, users who need it can refer to it. you can traverse and process the attribute values of objects and arrays.

Instructions for Use

The effect of the each function based on the parameter type is different:

1. Traverse objects (with additional parameters)

The code is as follows:


$. Each (Object, function (p1, p2 ){

This; // this indicates the current attribute value of the Object in each traversal.
P1; p2; // additional access parameters

}, ['Parameter 1', 'parameter 2']);


2. traverse the array (with attachment parameters)

The code is as follows:


$. Each (Array, function (p1, p2 ){

This; // this indicates the current element of the Array in each traversal.
P1; p2; // additional access parameters
}, ['Parameter 1', 'parameter 2']);


3. Traverse objects (no additional parameters)

The code is as follows:


$. Each (Object, function (name, value ){

This; // this indicates the value of the current property.
Name; // name indicates the name of the current property of the Object.
Value; // value indicates the value of the current property of the Object.
});
[Code]

4. traverse the array (no additional parameters)
[Code]
$. Each (Array, function (I, value ){

This; // this points to the current element
I; // I indicates the current subscript of Array
Value; // value indicates the current element of the Array.

});


The following describes some common usage of jQuery's each method.

The code is as follows:


Var arr = ["one", "two", "three", "four"];
$. Each (arr, function (){
Alert (this );
});
// The above each output results are: one, two, three, four

Var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]
$. Each (arr1, function (I, item ){
Alert (item [0]);
});
// In fact, arr1 is a two-dimensional array, and item is equivalent to taking every one-dimensional array,
// Item [0] is relative to the first value in each one-dimensional array.
// Therefore, the above each output is: 1 4 7


Var obj = {one: 1, two: 2, three: 3, four: 4 };
$. Each (obj, function (key, val ){
Alert (obj [key]);
});
// This each is even more powerful and can loop through every attribute.
// Output result: 1 2 3 4

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.