-1

while using jQuery each:

$.each([ 52, 97 ], function( index, value ) {
  alert( index + ": " + value );
});

Is there anything like continue to stop the loop?

Pseudocode:

$.each([ 52, 97, 10, 20, 30 ], function( index, value ) {
  alert( index + ": " + value );
  if (value==20) {
      continue; // STOP LOOPING AND CONTINUE AT POINT (2)
  }
});
<---- POINT (2) ---->
M.E.
  • 4,955
  • 4
  • 49
  • 128
  • 1
    Please read the documentation of `each` first. Should be the part of a research you do before asking a question. – trincot Feb 06 '18 at 18:59
  • 2
    Possible duplicate of [How to break out of jQuery each Loop](https://stackoverflow.com/questions/1784780/how-to-break-out-of-jquery-each-loop) – trincot Feb 06 '18 at 19:00

3 Answers3

3

To break each loop use return false:

$.each([ 52, 97, 10, 20, 30 ], function( index, value ) {
  alert( index + ": " + value );
  if (value==20) {
      return false;
  }
});

continue do same like return true and it skip immediately to the next iteration.

mozkomor05
  • 1,367
  • 11
  • 21
2

$.each() takes return false as a break statement:

$.each([ 52, 97, 10, 20, 30 ], function( index, value ) {
  alert( index + ": " + value );
  if (value===20) {
       return false;
  }
});

console.log("got the the rest of the script.")
CodeAt30
  • 874
  • 6
  • 17
0

Use return: false;

$.each([ 52, 97 ], function( index, value ) {
  alert( index + ": " + value );
  if (value == 52) { return false; }
});
Tallboy
  • 12,847
  • 13
  • 82
  • 173