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