const array = [1, 2, 3, 4, 5];
array.forEach(element => {
if (element === 3) {
return; // 模拟退出循环
}
console.log(element);
});
在JavaScript中,forEach方法是用于遍历数组的,它没有内建的机制来直接退出循环。不过,你可以使用return语句来模拟退出循环的效果。当return语句被执行时,它会中断当前的迭代并立即返回到调用forEach的代码处。
本文解释了在JavaScript的forEach方法中,如何通过使用return语句来模拟在遇到特定条件时退出循环的过程。
910

被折叠的 条评论
为什么被折叠?



