forEach()中无法return和break,代替方法some()与every()
some()当内部return true时跳出整个循环
var arr = [1,2,3,4,5];
var num = 3;
arr.some(function(v){
if(v == num) {
return true;
}
console.log(v);
});
every()当内部return false时跳出整个循环
var arr = [1,2,3,4,5];
var num = 3;
arr.every(function(v){
if(v == num) {
return false;
}else{
console.log(v);
return true;
}
});
作者:lihefei_coder
来源:CSDN
原文:https://blog.csdn.net/lihefei_coder/article/details/76736296
版权声明:本文为博主原创文章,转载请附上博文链接!
网友评论