关于javascript:一些js小技巧

退出forEach

try {
  var arr = [1, 2, 3, 4];
  arr.forEach(function (item, index) {
    //跳出条件
    if (item === 3) {
      throw new Error("LoopTerminates");
    }
    //do something
    console.log(item);
  });
} catch (e) {
  if (e.message !== "LoopTerminates") throw e;
};

参考链接:缩小for的应用

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理