try{
//是否跳出循环
let breakloop = false;
this.commitList = this.commitList.filter((arg)=>{
//是否跳出循环
if(breakloop)
throw new error("break");
//如果找到要删除的元素
if(arg == check.id)
//设为true 则下一次Loop会抛出异常
breakloop = true;
//找到元素 会返回false,在filter遍历中返回false会'删除'该元素
return arg != check.id
})
}catch(e){
console.log(e.message)
}
commitList是一个Array ,
假设
commitList[1,2,3,4,5,6]
check.id = 1
希望在找个1之后就不再进行下面的遍历,但是filter遍历不允许使用break字段
解决方法:
找到要删除的元素之后,抛出异常,强行跳出遍历
遇到问题:
跳出遍历后,数组还是维持不变(没有删除应该删除的元素),
可能是强行跳出打断了filter的工作?
请问有更好的方法吗? 既可以删除元素,又不需要全数组遍历..
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
为什么不用
https://developer.mozilla.org...