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

发布于 2024-07-12 08:54:42 字数 330 浏览 5 评论 0原文

我正在尝试迭代一系列元素。 jQuery 的文档说:

jquery.Each() 文档

返回非 false 与 for 循环中的 continue 语句相同,它会立即跳到下一次迭代。

我尝试过调用“return non-false;” 和“非虚假”; (无返回)两者都不会跳到下一次迭代。 相反,他们打破了循环。 我缺少什么?

I'm trying to iterate through an array of elements. jQuery's documentation says:

jquery.Each() documentation

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

千纸鹤带着心事 2024-07-19 08:54:42

他们所说的非假的意思是:

return true;

所以这段代码:

var arr = ["one", "two", "three", "four", "five"];
$.each(arr, function(i) {
  if (arr[i] == 'three') {
    return true;
  }
  console.log(arr[i]);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

将记录

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;
  }
  console.log(arr[i]);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

will log one, two, four, five.

莫相离 2024-07-19 08:54:42

通过“返回非假”,它们意味着返回任何不能计算为布尔值假的值。 因此,您可以返回 true1'non-false' 或您能想到的任何其他值。

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.

懵少女 2024-07-19 08:54:42

如果您按字面意思返回false,则循环会中断。 例如:

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

这意味着您可以返回任何其他内容,包括 undefined,如果您不返回任何内容,则返回的内容,因此您可以简单地使用空的 return 语句:

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

   // otherwise do something
});

这可能会因版本而异; 这适用于 jquery 1.12.4。 但实际上,当您退出函数的底部时,您也不会返回任何内容,这就是循环继续的原因,因此我希望不存在任何返回任何内容都无法继续的可能性。环形。 除非他们想强迫每个人都开始返回一些东西来保持循环继续,否则不返回任何东西必须才能保持循环继续下去。

The loop only breaks if you return literally 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:

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

可是我不能没有你 2024-07-19 08:54:42

Javascript 有“真实”和“虚假”的概念。 如果一个变量有一个值,那么通常是 9(如您所见),它具有“真实性”- null,或者没有值倾向于“虚假性”。 下面的片段可能会有所帮助:

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

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

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

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

希望有帮助吗?

另外,值得一看 Douglas Crockford

更新中的这些视频:感谢 @cphpython 发现损坏的链接 - 我现在已经更新以指向工作版本

Javascript 语言

Javascript - 好的部分

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:

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

update: thanks @cphpython for spotting the broken links - I've updated to point at working versions now

The Javascript language

Javascript - The Good Parts

平安喜乐 2024-07-19 08:54:42

不要忘记,有时您可以从块的末尾掉下来进入下一次迭代:

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

而不是像这样实际返回:

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

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 ...
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文