如何中断for 、foreach如何中断

102 阅读1分钟

中断for

  • break 离职 中止
  • continue 请一天假 跳过当前这一次循环
  • return 如果for循环在函数体中,也可以通过return中止

中断foreach

forEach中用return是没办法中断的,只能通过抛出异常的方法

const arr = [2, 3, 4]
arr.forEach(item => {
    console.log(item);
    throw new Error()
})