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


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;  
    })); 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM