翼度科技»论坛 编程开发 JavaScript 查看内容

JavaScript 中断 forEach 循环

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
1、使用 Array.prototype.some() 方法代替
some() 方法会在找到第一个符合条件的元素时停止循环。
例如:
  1. let array = [1, 2, 3, 4, 5];
  2. array.some(function(element, index, array) {
  3.     if (element === 3) {
  4.         console.log("Found 3 at index " + index);
  5.         return true;
  6.     }
  7. });
复制代码
上述代码会在找到第一个符合条件的元素(即 3)时停止循环。
2、使用 Array.prototype.every() 方法代替
  1. let array = [1, 2, 3, 4, 5];
  2. let stop = array.every(function(element) {
  3.     console.log(element);
  4.     if (element === 3) {
  5.         console.log("Found 3 at index ");
  6.         return false;
  7.     }
  8.     return true;
  9. });
复制代码
上述代码会在找到第一个符合条件的元素(即 3)时停止循环。
请注意,该方法找到的元素不会返回,需要在回调中自己处理。
3、使用 for循环代替
  1. let array = [1, 2, 3, 4, 5];
  2. for(let i = 0; i < array.length; i++) {
  3.     if (array[i] === 3) {
  4.         console.log("Found 3 at index " + i);
  5.         break;
  6.     }
  7. }
复制代码
上述代码也会在找到第一个符合条件的元素(即 3)时停止循环。
4、使用 try-catch 结构
可以在 forEach 循环内部使用 throw 语句来中断循环,并在外部使用 catch 语句来捕获该异常。
例如:
  1. let array = [1, 2, 3, 4, 5];
  2. try {
  3.     array.forEach(function(element, index, array) {
  4.         if (element === 3) {
  5.             console.log("Found 3 at index " + index);
  6.             throw "StopIteration";
  7.         }
  8.     });
  9. } catch (e) {
  10.     if (e !== "StopIteration") throw e;
  11. }
复制代码
上述代码会在找到第一个符合条件的元素(即 3)时停止循环。
请注意,使用 throw 语句中断 forEach 循环可能会使代码变得更加复杂,并且容易出现错误。因此,如果可能的话,应该使用前面提到的 Array.prototype.some() 或 for 循环来代替。
5、使用自定义的迭代器函数
  1. let array = [1, 2, 3, 4, 5];
  2. function forEach(array, callback) {
  3.     for (let i = 0; i < array.length; i++) {
  4.         callback(array[i], i, array);
  5.         if (array[i] === 3) {
  6.             console.log("Found 3 at index " + i);
  7.             break;
  8.         }
  9.     }
  10. }
  11. forEach(array, function(element, index, array) {
  12.     console.log(element);
  13. });
复制代码
上述代码会在找到第一个符合条件的元素(即 3)时停止循环。
这种方法虽然不够简洁,但是它可以在不改变原来的forEach函数的情况下,增加新的功能。
6、使用 Array.prototype.find() 或 Array.prototype.findIndex() 方法代替
find() 方法会在找到符合条件的第一个元素后返回该元素,并停止循环。
例如:
  1. let array = [1, 2, 3, 4, 5];
  2. let found = array.find(function(element) {
  3.     return element === 3;
  4. });
  5. console.log(found);
复制代码
上述代码会在找到第一个符合条件的元素(即 3)时停止循环并返回该元素。
Array.prototype.findIndex() 方法会在找到符合条件的第一个元素后返回该元素的索引,并停止循环。
例如:
  1. let array = [1, 2, 3, 4, 5];
  2. let index = array.findIndex(function(element) {
  3.     return element === 3;
  4. });
  5. console.log(index);
复制代码
上述代码会在找到第一个符合条件的元素(即 3)时停止循环并返回该元素的索引。
使用 Array.prototype.find() 或 Array.prototype.findIndex() 方法可以在 forEach 循环中找到符合条件的第一个元素并停止循环。这两种方法是在找到符合条件的元素后返回该元素或索引,而不是像 some() 方法或 for 循环中需要再次返回一个布尔值或使用 throw 语句来中断循环。
 
总之,主要方法还是通过其它方式代替 forEach 循环的中断,只有方法4 使用 try-catch 结构是实际意义上中断 forEach 循环。

来源:https://www.cnblogs.com/yuzhihui/p/17062872.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具