- forEach循环可以中断吗,如何中断
try {
[1,2,3,4,5,6,7].forEach((element, index) => {
if(element === 5) {
console.log(element)
throw new Error('中断循环');
}else{
console.log(element)
}
// 其他逻辑
});
} catch (e) {
if (e.message !== '中断循环') {
throw e; // 重新抛出非中断异常
}
}
答案:抛出异常
2. 循环遍历使用splice去重数组会发生什么?
let arr =[1,2,3,4,5,6,7,8,9,10]
arr.forEach((item,index)=>{
if(index>4){
console.log(item,index)
arr.splice(index,1)
}
})
console.log(arr)
结果:
可以发现,循环遍历使用splice时,由于索引顺序改变,紧接着的会删不掉
后面网上有答案,自行搜索
- 一个页面访问多个域名如何实现跨域
- xss攻击如何预防
- 事件委托了解吗,e.currentTarge.value和e.targe.value的区别是什么,怎么阻止事件冒泡?
- http读取缓存的返回状态是什么?
- Access-Control-Allow-Origin会发送一个预检请求,它的作用是什么,而且使用他不能用cookie,怎么处理?