Nested jQuery.each() - continue/break

后端 未结 11 489
天涯浪人
天涯浪人 2020-12-05 03:52

Consider the following code:

    var sentences = [
        \'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\',
        \'Vivamus aliquet nisl quis          


        
相关标签:
11条回答
  • 2020-12-05 04:27

    Labeled Break

    outerloop:
    $(sentences).each(function() 
    {
        $(words).each(function(i) 
        {
            break; /* breaks inner loop */
        } 
        $(words).each(function(i)  
        {
            break outerloop; /* breaks outer loop */
        }
    }
    
    0 讨论(0)
  • 2020-12-05 04:28

    You should do this without jQuery, it may not be as "pretty" but there's less going on and it's easier to do exactly what you want, like this:

    var sentences = [
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'Vivamus aliquet nisl quis velit ornare tempor.',
        'Cras sit amet neque ante, eu ultrices est.',
        'Integer id lectus id nunc venenatis gravida nec eget dolor.',
        'Suspendisse imperdiet turpis ut justo ultricies a aliquet tortor ultrices.'
    ];
    
    var words = ['ipsum', 'amet', 'elit'];
    
    for(var s=0; s<sentences.length; s++) {
        alert(sentences[s]);
        for(var w=0; w<words.length; w++) {
            if(sentences[s].indexOf(words[w]) > -1) {
                alert('found ' + words[w]);
                return;
            }
        }
    }
    

    You can try it out here. I'm not sure if this is the exact behavior you're after, but now you're not in a closure inside a closure created by the double .each() and you can return or break whenever you want in this case.

    0 讨论(0)
  • 2020-12-05 04:29

    There are a lot of answers here. And it's old, but this is for anyone coming here via google. In jQuery each function

    return false; is like break.

    just

    return; is like continue

    These will emulate the behavior of break and continue.

    0 讨论(0)
  • 2020-12-05 04:35

    Unfortunately no. The problem here is that the iteration happens inside functions, so they aren't like normal loops. The only way you can "break" out of a function is by returning or by throwing an exception. So yes, using a boolean flag seems to be the only reasonable way to "break" out of the outer "loop".

    0 讨论(0)
  • 2020-12-05 04:45

    return true not work

    return false working

    found = false;
    query = "foo";
    
    $('.items').each(function()
    {
      if($(this).text() == query)
      {
        found = true;
        return false;
      }
    });
    
    0 讨论(0)
  • 2020-12-05 04:46

    Confirm in API documentation http://api.jquery.com/jQuery.each/ say:

    We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

    and this is my example http://jsfiddle.net/r6jqP/

    (function($){
        $('#go').on('click',function(){
            var i=0,
                all=0;
            $('li').each(function(){
                 all++;
                 if($('#mytext').val()=='continue')return true;
                 i++;
                 if($('#mytext').val()==$(this).html()){
                     return false;
                 }
            });
            alert('Iterazione : '+i+' to '+all);
        });
    }(jQuery));
    
    0 讨论(0)
提交回复
热议问题