Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing `continue;` keywords in jQuery's `each();` method

Imagine that I want to loop over a list of jQuery objects, and for each of them execute some functions. However, if in some place, a criteria is not met, then I simply want to continue; to other objects. The simple pseudo-code might be something like:

$('#element').each(function(){
    // some code here
    if (!$(this).is('.included')) {
       continue; // This keyword of course, doesn't work here
    }
    // more code here, which won't get executed for some elements
});

I wan to achieve the same effect as:

for (var i = 0; i < 10; i++) {
    if (i % 2 == 0 ) {
        continue;
    }
    console.log(i);
}

I know that I can return false; somewhere inside each(); method, to completely break the looping. But I don't want to break looping. I only want to skip some elements. I also know that I can use if-else blocks to skip elements, but is there a more neat way to do this?

like image 306
Saeed Neamati Avatar asked May 06 '26 12:05

Saeed Neamati


1 Answers

simply use return; instead of continue; (not return false;).

like image 104
Molecular Man Avatar answered May 09 '26 02:05

Molecular Man



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!