原因:forEach()无法在所有元素都传递给调用的函数之前终止遍历
解决办法 使用 try catch
try {
arr.forEach(item =>{
if(item == '条件') {
// return 无效
// 使用 throw
throw new Error("End Loop");
}
})
} catch(e) {
if(e.message === 'End Loop') throw e
}
微信扫一扫
原因:forEach()无法在所有元素都传递给调用的函数之前终止遍历
解决办法 使用 try catch
try {
arr.forEach(item =>{
if(item == '条件') {
// return 无效
// 使用 throw
throw new Error("End Loop");
}
})
} catch(e) {
if(e.message === 'End Loop') throw e
}
相关推荐