无法从数组foreach循环中断[重复]

时间:2022-01-06 19:06:12

This question already has an answer here:

这个问题在这里已有答案:

   var statusArray = new Array();

   statusArray.forEach(function (item) {                
                 if (checkStatus != item) {
                     disableall = true;
                     return false;
                }
             });

I am returning false but not able break from foreach loop.

我正在返回false,但无法从foreach循环中断开。

1 个解决方案

#1


1  

There is no way to do break a forEach() loop

没有办法打破forEach()循环

Note : There is no way to stop or break a forEach loop. The solution is to use Array.every or Array.some. See example below.

注意:无法停止或中断forEach循环。解决方案是使用Array.every或Array.some。见下面的例子。

Since you have tagged it using jQuery, use $.each() if possible

由于您使用jQuery对其进行了标记,因此如果可能,请使用$ .each()

var statusArray = new Array();

$.each(statusArray, function (i, item) {
    if (checkStatus != item) {
        disableall = true;
        return false;
    }
});

Else try Array.every()/Array.some() as given in MDN

否则尝试在MDN中给出的Array.every()/ Array.some()

#1


1  

There is no way to do break a forEach() loop

没有办法打破forEach()循环

Note : There is no way to stop or break a forEach loop. The solution is to use Array.every or Array.some. See example below.

注意:无法停止或中断forEach循环。解决方案是使用Array.every或Array.some。见下面的例子。

Since you have tagged it using jQuery, use $.each() if possible

由于您使用jQuery对其进行了标记,因此如果可能,请使用$ .each()

var statusArray = new Array();

$.each(statusArray, function (i, item) {
    if (checkStatus != item) {
        disableall = true;
        return false;
    }
});

Else try Array.every()/Array.some() as given in MDN

否则尝试在MDN中给出的Array.every()/ Array.some()