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

时间:2021-01-26 13:42:32

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?

我尝试过调用“返回非false”和“非false”(不返回),它们都不会跳到下一个迭代。相反,他们打破了这个循环。我缺少什么?

6 个解决方案

#1


686  

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

会提醒1 2 4 5吗

#2


59  

By 'return non-false', they mean to return any value which would not work out to boolean false. So you could return true, 1, 'non-false', or whatever else you can think up.

通过“返回非false”,它们的意思是返回任何不符合布尔错误的值。所以你可以返回true, 1,非false,或者其他你能想到的。

#3


5  

Javascript sort of has the idea of 'truthiness' and 'falsiness'. If a variable has a value then, generally 9as you will see) it has 'truthiness' - null, or no value tends to 'falsiness'. The snippets below might help:

Javascript有“真实”和“虚假”的概念。如果一个变量有一个值,通常是9)它有“truthiness”——null,或者没有任何值趋向于“虚假”。下面的片段可能会有帮助:

var temp1; 
if ( temp1 )...  // false

var temp2 = true;
if ( temp2 )...  // true

var temp3 = "";
if ( temp3 ).... // false

var temp4 = "hello world";
if ( temp4 )...  // true

Hopefully that helps?

希望这可以帮助吗?

Also, its worth checking out these videos from Douglas Crockford

同样,值得一看道格拉斯·克罗克福德的这些视频

The Javascript language

Javascript语言

Javascript - The Good Parts

Javascript——好的部分。

#4


3  

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 ...
});

#5


1  

The loop only breaks if you return literally false. Ex:

只有当返回字面上为false时,循环才会中断。例:

// 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:

这意味着您可以返回任何其他内容,包括未定义的内容,如果您没有返回任何内容,则返回该内容,因此您可以简单地使用一个空返回语句:

$.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. 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.

这可能因版本不同而不同;这适用于jquery 1.12.4。但实际上,当你退出函数的底部时,你也没有返回任何东西,这就是循环继续的原因,所以我认为没有任何可能返回的东西不能继续循环。除非他们想迫使每个人开始返回一些东西以保持循环继续,否则返回任何东西都不能保证循环继续。

#6


-2  

jQuery.noop() can help

jQuery.noop()可以帮助

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

#1


686  

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

会提醒1 2 4 5吗

#2


59  

By 'return non-false', they mean to return any value which would not work out to boolean false. So you could return true, 1, 'non-false', or whatever else you can think up.

通过“返回非false”,它们的意思是返回任何不符合布尔错误的值。所以你可以返回true, 1,非false,或者其他你能想到的。

#3


5  

Javascript sort of has the idea of 'truthiness' and 'falsiness'. If a variable has a value then, generally 9as you will see) it has 'truthiness' - null, or no value tends to 'falsiness'. The snippets below might help:

Javascript有“真实”和“虚假”的概念。如果一个变量有一个值,通常是9)它有“truthiness”——null,或者没有任何值趋向于“虚假”。下面的片段可能会有帮助:

var temp1; 
if ( temp1 )...  // false

var temp2 = true;
if ( temp2 )...  // true

var temp3 = "";
if ( temp3 ).... // false

var temp4 = "hello world";
if ( temp4 )...  // true

Hopefully that helps?

希望这可以帮助吗?

Also, its worth checking out these videos from Douglas Crockford

同样,值得一看道格拉斯·克罗克福德的这些视频

The Javascript language

Javascript语言

Javascript - The Good Parts

Javascript——好的部分。

#4


3  

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 ...
});

#5


1  

The loop only breaks if you return literally false. Ex:

只有当返回字面上为false时,循环才会中断。例:

// 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:

这意味着您可以返回任何其他内容,包括未定义的内容,如果您没有返回任何内容,则返回该内容,因此您可以简单地使用一个空返回语句:

$.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. 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.

这可能因版本不同而不同;这适用于jquery 1.12.4。但实际上,当你退出函数的底部时,你也没有返回任何东西,这就是循环继续的原因,所以我认为没有任何可能返回的东西不能继续循环。除非他们想迫使每个人开始返回一些东西以保持循环继续,否则返回任何东西都不能保证循环继续。

#6


-2  

jQuery.noop() can help

jQuery.noop()可以帮助

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