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