ForEach exits the loop in JavaScript (js)

ForEach exits the loop in JavaScript

In JavaScript, forEach loop, break and return are both unable to jump out of the loop, you need to use the method of throwing an exception to jump out of the loop

code show as below:

try {
    
    
	BarCodeList.forEach(lst => {
    
    
		if (lst.SNO == item.SNO) {
    
    
			
			抛出异常,跳出循环
			throw new Error("EndIterative");			
		}
	});
} catch (e) {
    
    
	if (e.message != "EndIterative") {
    
    
			mui.alert("数据获取异常请重试!");
			throw e;
	}
}

Guess you like

Origin blog.csdn.net/qq_37192571/article/details/109309724