To use jQuery's $.each() method for iterating over arrays and objects, you can follow these steps:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
var myArray = [1, 2, 3, 4, 5];
var myObject = {
key1: 'value1',
key2: 'value2',
key3: 'value3'
};
$.each(myArray, function(index, value) {
console.log(index + ': ' + value);
});
$.each(myObject, function(key, value) {
console.log(key + ': ' + value);
});
In the $.each() method, the first parameter represents the array or object to iterate over. The second parameter is a callback function that will be executed for each item in the array or each property in the object. The callback function has two parameters: index
and value
for arrays, and key
and value
for objects.