How to skip to next iteration in jQuery.each() util?

Josh picture Josh · Jan 26, 2009 · Viewed 257.3k times · Source

I'm trying to iterate through an array of elements. jQuery's documentation says:

jquery.Each() documentation

Returning non-false is the same as a continue statement in a for loop, it will skip immediately to the next iteration.

I've tried calling 'return non-false;' and 'non-false;' (sans return) neither of which skip to the next iteration. Instead, they break the loop. What am i missing?

Answer

Paolo Bergantino picture Paolo Bergantino · Jan 26, 2009

What they mean by non-false is:

return true;

So this code:

var arr = ["one", "two", "three", "four", "five"];
$.each(arr, function(i) {
  if (arr[i] == 'three') {
    return true;
  }
  console.log(arr[i]);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

will log one, two, four, five.