转到“下一步" JavaScript forEach循环中的迭代 [英] Go to "next" iteration in JavaScript forEach loop

查看:64
本文介绍了转到“下一步" JavaScript forEach循环中的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何进入JavaScript Array.forEach()循环的下一个迭代?

How do I go to the next iteration of a JavaScript Array.forEach() loop?

例如:

var myArr = [1, 2, 3, 4];

myArr.forEach(function(elem){
  if (elem === 3) {
    // Go to "next" iteration. Or "continue" to next iteration...
  }

  console.log(elem);
});

MDN文档仅提及完全脱离循环,不进行下一个迭代.

MDN docs only mention breaking out of the loop entirely, not moving to next iteration.

推荐答案

如果要跳过当前迭代,可以简单地return.

You can simply return if you want to skip the current iteration.

由于您处于函数中,因此如果在执行其他任何操作之前先执行return,则实际上已经跳过了return语句下面的代码的执行.

Since you're in a function, if you return before doing anything else, then you have effectively skipped execution of the code below the return statement.

这篇关于转到“下一步" JavaScript forEach循环中的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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