Through it, you can iterate over the property values of objects and arrays and process them.
Instructions for use
The effect of each function is not completely consistent according to the type of parameter:
1. Traversing the object (with additional parameters)
$.each(Object, function(p1, p2) {this; //This here points to the current property value of Object in each traversal; p2; //Access additional parameters}, ['parameter 1', 'parameter 2']);2. Iterate over the array (with attachment parameters)
$.each(Array, function(p1, p2){this; //This here points to the current element of Array in each traversal; p2; //Access additional parameters}, ['parameter 1', 'parameter 2']);3. Traversing the object (no additional parameters)
$.each(Object, function(name, value) {this; //this points to the value of the current attribute; //name represents the name of the current attribute of the Object; //value represents the value of the current attribute of the Object});4. Iterate over the array (no additional parameters)
$.each(Array, function(i, value) {this; //this points to the current element i; //i represents the current subscript value of Array; //value represents the current element of Array});Let me mention several common uses of jQuery's each method
Js code
var arr = [ "one", "two", "three", "four"]; $.each(arr, function(){ alert(this); }); //The results of the above each output 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, item is equivalent to taking each one-dimensional array, //item[0] is relative to taking the first value in each one-dimensional array//So 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 every attribute//The output result is: 1 2 3 4There are two kinds of people born to be jealous, one is an art madman, the other is a code madman...
Jealousy is what drives me forward