Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • $.each loop

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 183
    Comment on it

    $.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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: