JavaScript(js)中forEach退出循环

JavaScript中forEach退出循环

JavaScript中forEach循环,break与return都是无法跳出循环,需要使用抛出异常的方式跳出循环

代码如下:

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

猜你喜欢

转载自blog.csdn.net/qq_37192571/article/details/109309724