如何跳转到jQuery.each()util中的下一个迭代?

本文翻译自:How to skip to next iteration in jQuery.each() util?

I'm trying to iterate through an array of elements. 我正在尝试迭代一系列元素。 jQuery's documentation says: jQuery的文档说:

jquery.Each() documentation jquery.Each()文档

Returning non-false is the same as a continue statement in a for loop, it will skip immediately to the next iteration. 返回非false与for循环中的continue语句相同,它将立即跳到下一次迭代。

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? 我错过了什么?


#1楼

参考:https://stackoom.com/question/21Hl/如何跳转到jQuery-each-util中的下一个迭代


#2楼

Dont forget that you can sometimes just fall off the end of the block to get to the next iteration: 不要忘记你有时可以从块的末尾掉下来进行下一次迭代:

$(".row").each( function() {
    if ( ! leaveTheLoop ) {
        ... do stuff here ...
    }
});

Rather than actually returning like this: 而不是像这样实际返回:

$(".row").each( function() {
    if ( leaveTheLoop ) 
        return; //go to next iteration in .each()
    ... do stuff here ...
});

#3楼

jQuery.noop() can help jQuery.noop()可以提供帮助

$(".row").each( function() {
    if (skipIteration) {
        $.noop()
    }
    else{doSomething}
});

#4楼

The loop only breaks if you return literally false . 如果你返回字面上的false ,循环只会中断。 Ex: 例如:

// this is how jquery calls your function
// notice hard comparison (===) against false
if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
   break;
}

This means you can return anything else, including undefined , which is what you return if you return nothing, so you can simply use an empty return statement: 这意味着你可以返回任何其他东西,包括undefined ,如果你什么都不返回就会返回,所以你可以简单地使用一个空的return语句:

$.each(collection, function (index, item) {
   if (!someTestCondition)
      return; // go to next iteration

   // otherwise do something
});

It's possible this might vary by version; 这可能因版本而异; this is applicable for jquery 1.12.4. 这适用于jquery 1.12.4。 But really, when you exit out the bottom of the function, you are also returning nothing, and that's why the loop continues, so I would expect that there is no possibility whatsoever that returning nothing could not continue the loop. 但实际上,当你退出函数的底部时,你也没有返回任何东西,这就是循环继续的原因,所以我希望没有任何可能返回的东西不能继续循环。 Unless they want to force everyone to start returning something to keep the loop going, returning nothing has to be a way to keep it going. 除非他们想迫使大家开始返回的东西,以保持循环下去,回到什么事都没有要继续下去的方式。


#5楼

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;
    }
    alert(arr[i]);
});

Will alert one, two, four, five 会警告一,二,四,五


#6楼

By 'return non-false', they mean to return any value which would not work out to boolean false. 通过'return non-false',它们意味着返回任何不能用于布尔值false的值。 So you could return true , 1 , 'non-false' , or whatever else you can think up. 所以你可以返回true1'non-false' ,或者你能想到的任何其他东西。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值