首页 > Web开发 > 详细

JS中every()和some()的用法

时间:2021-06-01 16:57:30      阅读:29      评论:0      收藏:0      [点我收藏+]
 

every()与some()方法都是JS中数组的迭代方法。

every()是对数组中每一项运行给定函数,如果该函数对每一返回true,则返回true。

some()是对数组中每一项运行给定函数,如果该函数对任一返回true,则返回true。

 

var arr = [ 1, 2, 3, 4, 5, 6 ]; 
 
console.log( arr.some( function( item, index, array ){ 
    console.log( ‘item=‘ + item + ‘,index=‘+index+‘,array=‘+array ); 
    return item > 3; 
})); 
 
console.log( arr.every( function( item, index, array ){ 
    console.log( ‘item=‘ + item + ‘,index=‘+index+‘,array=‘+array ); 
    return item > 3; 
}));

 

var arr = [ 1, 2, 3, 4, 5, 6 ]; 
 
console.log( arr.some( function( item, index, array ){ 
    console.log( ‘item=‘ + item + ‘,index=‘+index+‘,array=‘+array ); 
    return item > 3; 
})); 
 
console.log( arr.every( function( item, index, array ){ 
    console.log( ‘item=‘ + item + ‘,index=‘+index+‘,array=‘+array ); 
    return item > 3; 
}));

JS中every()和some()的用法

原文:https://www.cnblogs.com/lijjj/p/14837473.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!