Home  >  Article  >  Web Front-end  >  How to exit forEach loop in JavaScript

How to exit forEach loop in JavaScript

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-04-29 09:07:184728browse

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.

How to exit forEach loop in JavaScript

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete