84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
JavaScript中forEach循环数组时,如何中途跳出循环?
ringa_lee
forEach是函数,不是语法,因此没有直接break的语法
曲线救国
var BreakException= {}; try { [1,2,3].forEach(function(i) { if(i === 2) throw BreakException; console.log(i); }); } catch(e) { if (e!==BreakException) throw e; }
[1,2,3].some(function(i) { if(i == 2) return true; console.log(i); });
没有办法中止 forEach 循环。如果要中止,可使用 Array.every 或 Array.some。见下面的例子。
来自:Array.prototype.forEach()
forEach是函数,不是语法,因此没有直接break的语法
曲线救国
来自:Array.prototype.forEach()