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

How to exit forEach loop in JavaScript

醉折花枝作酒筹
Release: 2021-04-29 09:07:18
forward
4828 people have browsed it

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;
	}}
Copy after login

[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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template