关于数组的forEach方法中达到continue和break效果
yulilong opened this issue · 1 comments
yulilong commented
老师你好,看你的教程:
https://wangdoc.com/javascript/stdlib/array.html#foreach
你在数组的forEach方法介绍中说 不能中断执行。
经过查找资料发现,可以通过return 语句来达到 continue和break 的效果:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arr.forEach((item) => {
if (item === 3) return true; // continue
if (item > 7) return false; // break
console.log(item);
})ruanyf commented
forEach 总是会遍历每一个值,break 不会。