Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery each - Stop loop and return object

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!     }      });   } 
like image 685
user970727 Avatar asked Nov 22 '11 09:11

user970727


People also ask

How do I get out of each loop in jQuery?

To break a $. each or $(selector). each loop, you have to return false in the loop callback. Returning true skips to the next iteration, equivalent to a continue in a normal loop.

What is the use of jQuery each () function?

each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

Can we use forEach in jQuery?

each() function, jQuery's foreach equivalent. jQuery's foreach equivalent can be very useful for many situations. These examples will get you started and teach you how you can loop through arrays, objects and all kinds of HTML elements.

How do you break a forEach?

There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.


2 Answers

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.

like image 156
James Allardice Avatar answered Sep 20 '22 08:09

James Allardice


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){     ... 
like image 39
Peter Avatar answered Sep 21 '22 08:09

Peter