如何停止 JavaScript for 循环? [英] How to stop a JavaScript for loop?

查看:23
本文介绍了如何停止 JavaScript for 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此 JavaScript 遍历数组并找到匹配的数组元素:

I'm using this JavaScript to iterate through an array and find a matching array element:

var remSize = [], 
    szString, remData, remIndex, i;

for (i = 0; i < remSize.length; i++) {      
    // I'm looking for the index i, when the condition is true
    remSize[i].size == remData.size ? remIndex = i : remIndex = -1;     
}

数组包含这些大小":["34", "36", "38"...].

The array contains these "sizes": ["34", "36", "38"...].

remData.size 是我正在寻找的大小"(例如36").

remData.size is the "size" I'm looking for (e.g. "36").

如果我要搜索的大小在索引中,我需要返回索引 i.否则我需要返回 -1.有没有更好的方法来做到这一点?

I need to return the index i if the size I'm searching for is in the index. Otherwise I need to return -1. Is there a better way to do this?

推荐答案

要在 JavaScript 中尽早停止 for 循环,您可以使用 break:

To stop a for loop early in JavaScript, you use break:

var remSize = [], 
    szString,
    remData,
    remIndex,
    i;

/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */

remIndex = -1; // Set a default if we don't find it
for (i = 0; i < remSize.length; i++) {      
     // I'm looking for the index i, when the condition is true
     if (remSize[i].size === remData.size) {
          remIndex = i;
          break;       // <=== breaks out of the loop early
     }
}

如果您在 ES2015(又名 ES6)环境中,对于这个特定用例,您可以使用 Array#findIndex(查找条目的索引)或Array#find(查找条目本身),两者都可以填充/填充:

If you're in an ES2015 (aka ES6) environment, for this specific use case, you can use Array#findIndex (to find the entry's index) or Array#find (to find the entry itself), both of which can be shimmed/polyfilled:

var remSize = [], 
    szString,
    remData,
    remIndex;

/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */

remIndex = remSize.findIndex(function(entry) {
     return entry.size === remData.size;
});

Array#find:

var remSize = [], 
    szString,
    remData,
    remEntry;

/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */

remEntry = remSize.find(function(entry) {
     return entry.size === remData.size;
});

Array#findIndex 在回调第一次返回真值时停止,返回该回调的索引;如果回调从不返回真值,则返回 -1.Array#find 在找到您要查找的内容时也会停止,但它会返回条目,而不是其索引(或者 undefined 如果回调从不返回真值).

Array#findIndex stops the first time the callback returns a truthy value, returning the index for that call to the callback; it returns -1 if the callback never returns a truthy value. Array#find also stops when it finds what you're looking for, but it returns the entry, not its index (or undefined if the callback never returns a truthy value).

如果您使用的是 ES5 兼容环境(或 ES5 shim),则可以使用新的 some 函数,它调用回调直到回调返回一个真值:

If you're using an ES5-compatible environment (or an ES5 shim), you can use the new some function on arrays, which calls a callback until the callback returns a truthy value:

var remSize = [], 
    szString,
    remData,
    remIndex;

/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */

remIndex = -1; // <== Set a default if we don't find it
remSize.some(function(entry, index) {
    if (entry.size === remData.size) {
        remIndex = index;
        return true; // <== Equivalent of break for `Array#some`
    }
});

如果你使用 jQuery,你可以使用 jQuery.each 遍历数组;看起来像这样:

If you're using jQuery, you can use jQuery.each to loop through an array; that would look like this:

var remSize = [], 
    szString,
    remData,
    remIndex;

/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */

remIndex = -1; // <== Set a default if we don't find it
jQuery.each(remSize, function(index, entry) {
    if (entry.size === remData.size) {
        remIndex = index;
        return false; // <== Equivalent of break for jQuery.each
    }
});

这篇关于如何停止 JavaScript for 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆