1.因為 forEach() 無法通過正常流程終止,所以可以通過拋出異常的方式實現終止。
try{
var array = ["first","second","third","fourth"];
// 執行到第3次,結束循環
array.forEach(function(item,index) {
if(item == "third"){
throw new Error("EndIterative");
}
console.log(item); // first second
});
}catch(e){
if(e.message != "EndIterative") throw e;
}
// 下面的代碼不影響繼續執行
console.log("繼續執行。。。");
.
