Exit jQuery $ .each loop

Possible duplicate:
How can I escape from $ .each in jquery?

How to exit jquery $ .each loop?

+3
source share
3 answers

Use return falseinside the loop .each()to break free. Returning everything that is not false seems to be continue: it stops the current iteration and proceeds directly to the next.

var myArr = [1,2,3,4,5,6,7];
$.each( myArr, function(){
  // Skip on three
  if( this === 3 ) return true;
  // Abort on five
  if( this === 5 ) return false;
  doStuff( this ); // never for 3, 5, 6 or 7
});
+10
source

http://api.jquery.com/jQuery.each/

We can break the $ .each () loop into in particular, by calling the callback function returns false. Returning non-false is the same as continuing in a for loop; This will immediately proceed to the next iteration.

+5
source

continue break , , jQuery , :

continue, return true .each.

break, return false.

+3
source

Source: https://habr.com/ru/post/1781302/


All Articles