Jquery each - Stop loop and return object

117,228

Solution 1

Because when you use a return statement inside an each loop, a "non-false" value will act as a continue, whereas false will act as a break. You will need to return false from the each function. Something like this:

function findXX(word) {
    var toReturn; 
    $.each(someArray, function(i) {
        $('body').append('-> '+i+'<br />');
        if(someArray[i] == word) {
            toReturn = someArray[i];
            return false;
        }   
    }); 
    return toReturn; 
}

From the docs:

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

Solution 2

modified $.each function

$.fn.eachReturn = function(arr, callback) {
   var result = null;
   $.each(arr, function(index, value){
       var test = callback(index, value);
       if (test) {
           result = test;
           return false;
       }
   });
   return result ;
}

it will break loop on non-false/non-empty result and return it back, so in your case it would be

return $.eachReturn(someArray, function(i){
    ...

Solution 3

here :

http://jsbin.com/ucuqot/3/edit

function findXX(word)
{  
  $.each(someArray, function(i,n)
  {
    $('body').append('-> '+i+'<br />');
    if(n == word)
    {
      return false;
    }   
  });  
}
Share:
117,228
user970727
Author by

user970727

Updated on July 05, 2022

Comments

  • user970727
    user970727 almost 2 years

    Can anybody tell me why the loop did not stop after the 5 entry?

    http://jsbin.com/ucuqot/edit#preview


    $(document).ready(function()
    {
      someArray = new Array();
      someArray[0] = 't5';
      someArray[1] = 'z12';
      someArray[2] = 'b88';
      someArray[3] = 's55';
      someArray[4] = 'e51';
      someArray[5] = 'o322';
      someArray[6] = 'i22';
      someArray[7] = 'k954';  
    
      var test =  findXX('o322');   
    
    });
    
    function findXX(word)
    {  
      $.each(someArray, function(i)
      {
        $('body').append('-> '+i+'<br />');
        if(someArray[i] == 'someArray')
        {
          return someArray[i]; //<--- did not stop the loop!
        }   
      });  
    }
    
  • Royi Namir
    Royi Namir over 12 years
    why dont you use the integrated function(i,n) ?
  • James Allardice
    James Allardice over 12 years
    Because I just used the code the OP posted in the question and changed the return.
  • James Allardice
    James Allardice over 12 years
    @user970727 - In that example you don't have the return statement in the each loop at all... Here's an updated example: jsbin.com/ucuqot/5/edit
  • pavan kumar
    pavan kumar over 7 years
    @JamesAllardice didn't work for me,at "return=false;" it is exiting the loop alright but later propagating to the statement "return toReturn;" where in my instance i'm returning "true".so true is always returned even if "return = false" is executed
  • kiwicomb123
    kiwicomb123 about 7 years
    Why is like that? you would think, return some value would imply break out of the loop.