$.each loop is the substitute of the age old for loop and for-in loop.
jquery provide an object iterator utitlity $.each() and collection iterator utitlity .each().
$.each is an generic iterator function for loop. It is used for both object and array and even array like object
example
var sum = 0;
var arr = [ 1, 2, 3, 4, 5 ];
Then in for loop
for ( var i = 0, l = arr.length; i < l; i++ ) {
sum += arr[ i ];
}
console.log( sum );
and in $.each
$.each( arr, function( index, value ){
sum += value;
});
console.log( sum );
We don't have to access array[index] as the value is passed automatically by $.each().
0 Comment(s)