How to break out of jQuery each Loop

后端 未结 7 1967
遇见更好的自我
遇见更好的自我 2020-11-22 10:04

How do I break out of a jQuery each loop?

I have tried:

 return false;

In the loop but this did not work. Any ideas?

相关标签:
7条回答
  • 2020-11-22 10:40

    "each" uses callback function. Callback function execute irrespective of the calling function,so it is not possible to return to calling function from callback function.

    use for loop if you have to stop the loop execution based on some condition and remain in to the same function.

    0 讨论(0)
  • 2020-11-22 10:48

    I use this way (for example):

    $(document).on('click', '#save', function () {
        var cont = true;
        $('.field').each(function () {
            if ($(this).val() === '') {
                alert('Please fill out all fields');
                cont = false;
                return false;
            }
        });
        if (cont === false) {
            return false;
        }
        /* commands block */
    });
    

    if cont isn't false runs commands block

    0 讨论(0)
  • 2020-11-22 11:00

    According to the documentation return false; should do the job.

    We can break the $.each() loop [..] by making the callback function return false.

    Return false in the callback:

    function callback(indexInArray, valueOfElement) {
      var booleanKeepGoing;
    
      this; // == valueOfElement (casted to Object)
    
      return booleanKeepGoing; // optional, unless false 
                               // and want to stop looping
    }
    

    BTW, continue works like this:

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

    0 讨论(0)
  • 2020-11-22 11:00

    I created a Fiddle for the answer to this question because the accepted answer is incorrect plus this is the first StackOverflow thread returned from Google regarding this question.

    To break out of a $.each you must use return false;

    Here is a Fiddle proving it:

    http://jsfiddle.net/9XqRy/

    0 讨论(0)
  • 2020-11-22 11:00

    I came across the situation where I met a condition that broke the loop, however the code after the .each() function still executed. I then set a flag to "true" with an immediate check for the flag after the .each() function to ensure the code that followed was not executed.

    $('.groupName').each(function() {
        if($(this).text() == groupname){
            alert('This group already exists');
            breakOut = true;
            return false;
        }
    });
    if(breakOut) {
        breakOut = false;
        return false;
    } 
    
    0 讨论(0)
  • 2020-11-22 11:01

    I know its quite an old question but I didn't see any answer, which clarify that why and when its possible to break with return.

    I would like to explain it with 2 simple examples:

    1. Example: In this case, we have a simple iteration and we want to break with return true, if we can find the three.

    function canFindThree() {
        for(var i = 0; i < 5; i++) {
            if(i === 3) {
               return true;
            }
        }
    }
    

    if we call this function, it will simply return the true.

    2. Example In this case, we want to iterate with jquery's each function, which takes anonymous function as parameter.

    function canFindThree() {
    
        var result = false;
    
        $.each([1, 2, 3, 4, 5], function(key, value) { 
            if(value === 3) {
                result = true;
                return false; //This will only exit the anonymous function and stop the iteration immediatelly.
            }
        });
    
        return result; //This will exit the function with return true;
    }
    
    0 讨论(0)
提交回复
热议问题