js中forEach跳出循环

forEach循环,break跳出不起作用,需要使用抛出异常的方式跳出循环

 try {
	this.diffList.forEach(item => {
	  if (item.id == '1') {
		//跳出循环

		//抛出异常,跳出循环
		throw new Error("EndIterative");
	  }
	});
  } catch (e) {
	if (e.message != "EndIterative") {
	  throw e;
	}
  }

猜你喜欢

转载自blog.csdn.net/sayoko06/article/details/88238642