jQuery $.each traverses instances of object array usage

  • 2020-05-30 19:20:19
  • OfStack

With it, you can traverse the property values of objects, arrays, and process them.

Directions for use

The each function is not completely implemented according to the type of parameter.

1. Traversing the object (with additional parameters)


$.each(Object, function(p1, p2) {      this;       // Here, this Point to each traversal Object The current property value of
     p1; p2;     // Access additional parameters }, [' parameter 1', ' parameter 2']);

2. Traversal group (with attached parameters)


$.each(Array, function(p1, p2){      this;       // Here, this Point to each traversal Array Current element of
     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 said Object The name of the current property
     value;     //value said Object The value of the current property
});
[code]
  4 , go through the groups ( No additional parameters )
[code]
$.each(Array, function(i, value) {      this;      //this Pointing to the current element
     i;         //i said Array The subscript
     value;     //value said Array The current element });

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


 var arr = [ "one", "two", "three", "four"];    
 $.each(arr, function(){    
    alert(this);    
 });    
// The above each The output results are as follows: one,two,three,four   
   
var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]]    
$.each(arr1, function(i, item){    
   alert(item[0]);    
});    
// Actually, arr1 for 1 a 2 Dimensional array, item That's the same thing as taking each 1 a 1 Dimensional array,   
//item[0] As opposed to taking each 1 a 1 The number in the dimension array 1 A value   
// So this one up here each The outputs are: 1   4   7    
 
 
var obj = { one:1, two:2, three:3, four:4};    
$.each(obj, function(key, val) {    
    alert(obj[key]);          
});  
// this each It's even more powerful. You can cycle through it 1 A property     
// The output result is: 1   2  3  4


Related articles: