如何使Array.forEach短路像调用break?
[1,2,3].forEach(function(el) {
    if(el === 1) break;
});
我怎样才能在JavaScript中使用新的forEach方法? 
  有没有内置在能力break在forEach 。  要中断执行,你必须抛出某种异常。  例如。 
var BreakException = {};
try {
  [1, 2, 3].forEach(function(el) {
    console.log(el);
    if (el === 2) throw BreakException;
  });
} catch (e) {
  if (e !== BreakException) throw e;
}你可以使用每种方法:
[1,2,3].every(function(el) {
    return !(el === 1);
});
对于旧的浏览器支持使用:
if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this &&
          !fun.call(thisp, this[i], i, this))
        return false;
    }
    return true;
  };
}
更多细节在这里。
现在有一种更好的方法可以在ECMAScript2015(又名ES6)中使用新的for循环来实现。 例如,此代码不会在数字5之后打印数组元素:
let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let el of arr) {
  console.log(el);
  if (el === 5) {
    break;
  }
}上一篇: How to short circuit Array.forEach like calling break?
下一篇: Why is using "for...in" with array iteration a bad idea?
