Home>Article>Web Front-end> How to exit forEach loop in JavaScript
This article will introduce to you how to exit the forEach loop in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
In the forEach loop in JavaScript, break and return cannot break out of the loop, and you need to use an exception to jump out of the loop
The code is as follows:
try { BarCodeList.forEach(lst => { if (lst.SNO == item.SNO) { 抛出异常,跳出循环 throw new Error("EndIterative"); } });} catch (e) { if (e.message != "EndIterative") { mui.alert("数据获取异常请重试!"); throw e; }}
[Recommended learning:javascript advanced tutorial]
The above is the detailed content of How to exit forEach loop in JavaScript. For more information, please follow other related articles on the PHP Chinese website!