forEach打断方法

问题:

多层循环嵌套如何打断循环进程

解决方法:

1.使用return 打断,但是这只是打断一次循环,如果嵌套循环的数据中有重复数据该方法就容易出现问题。
2.使用 try 通过抛出异常的方式实现终止循环(多层循环,数据有重复,推荐使用)

例:

checkAll.forEach((item,index)=>{
	try {
		that.Nodisabled.forEach((itemC,indexc)=>{
		if(item==itemC.title){//获取被选中的数据
			let newItme={
				...itemC,
				isAdd:false,
			}
			console.log(newItme,"newItme")
			that.Allarr.push(newItme);
			throw new Error('End');
			// 打断跳出循环
			}
		})
	} catch(e) {
		console.log(e);
	}
})

版权声明:本文为qq_37403179原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。