Examples of each traversing objects and arrays in jquery

  • 2020-03-30 03:39:12
  • OfStack

  Generic traversal method that can be used to traverse objects and arrays. $().each(), the callback function takes two arguments:

The first is the member of the object or the index of the array, and the second is the corresponding variable or content. Exit each loop to cause the callback function to return false

There are the following two selects


 Planning category : 
<select id="PLANTYPE"> 
<option value="0">- all -</option> 
<option value="1"> new </option> 
<option value="2"> Carrying on </option> 
</select> 
 Declare the type : 
<select id="AUDITTYPE"> 
<option value="0">- all -</option> 
<option value="1"> declare </option> 
<option value="2"> Modify the </option> 
</select>

Use the each method to get the text value in option, i.e. - all -, new, extended...

If you only use each loop once, start at option


$("option").each(function(index,data){ 
console.info($( data ).text()); 
//Or the console. The info ($(this). The text ());
})

You can also start at select


$("select").each(function( index,data){ 
$("option", data).each(function(m,n){ 
console.info($(this).text()); 
}) 
})

$("option", data) must be added to data or $("option",this) to indicate the option under this object

Otherwise it's all options.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - gorgeous line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Each is also used in jQuery. Each (object, [callback])
Unlike the $().each() method of a jQuery object, this method can be used to instantiate any object.

Iterate through the code in the same way


$.each($("option"),function(index,data){ 
console.info(index+" "+data); 
})

You can also iterate over groups


$.each( [0,1,2], function(i, n){ 
console.info( "Index : " + i + ": " + n ); 
});

Traverse object


$.each({ name: "itmyhome", addr: "Beijing" },function(i, n){ 
console.info("Name: " + i + ", Value: " + n); 
});


Related articles: