By $. each can traverse and process the attribute values of objects and arrays. The following is an example. If you need it, you 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)
$. Each (Object, function (p1, p2) {this; // here this points to the current property value of the Object in each traversal p1; p2; // access additional parameter }, ['parameter 1', 'parameter 2']);
2. traverse the Array (with attachment parameters)
$. Each (Array, function (p1, p2) {this; // here this points to the current element p1; p2 in each traversal; // access additional parameter }, ['parameter 1', 'parameter 2']);
3. Traverse objects (no additional parameters)
$. Each (Object, function (name, value) {this; // this points to the value name of the current attribute; // name indicates the name value of the current attribute of the Object; // value indicates the value of the current property of the Object });
4. traverse the Array (no additional parameters)
$. Each (Array, function (I, value) {this; // this points to the current element I; // I indicates the current subscript value of the Array; // value indicates the current element of the Array });
The following describes some common usage of jQuery's each method.
Js Code
Var arr = ["one", "two", "three", "four"]; $. each (arr, function () {alert (this) ;}); // The above each Outputs 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. // 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 more powerful, can loop every attribute // The output result is: 1 2 3 4
Naturally, they will envy two types of people. One is an artistic madman, and the other is a code madman ......
Jealousy is the driving force for me to move forward