$.each traverses and processes the property values of the object the array

  • 2020-03-30 03:34:23
  • OfStack

It allows you to iterate over the values of objects, arrays, and processes.

Directions 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 refers to the current property value of the Object in each iteration
p1; p2; //Access additional parameters
}, [' parameter 1', ' parameter 2']);

2. Traversal groups (with attachment parameters)


$.each(Array, function(p1, p2){
this; //This here refers to the current element of the Array in each iteration
p1; 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 property
name; //Name represents the name of the current property of the Object
value; //Value represents the value of the current property of the Object
});

4. Iterate over groups (no additional parameters)


$.each(Array, function(i, value) {
this; //This points to the current element
i; //I represents the current index of the Array
value; //Value represents the current element of the Array
});

Here are a few common USES of jQuery's each method

Js code


var arr = [ "one", "two", "three", "four"]; 
$.each(arr, function(){ 
alert(this); 
}); 
//The output of each above is: one,two,three,four

var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]] 
$.each(arr1, function(i, item){ 
alert(item[0]); 
}); 
//So arr1 is actually a two dimensional array, so item is equal to taking each one dimensional array,
//Item [0] relative to the first value in each one-dimensional array
//So the output of each above is 1, 4, and 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. It loops through each property
// The output result is: 1 2 3 4

Naturally jealous of two kinds of people, those who are art maniacs and those who are code maniacs...
Jealousy is what keeps me going


Related articles: