JS中every()和some()的用法數組
every()與some()方法都是JS中數組的迭代方法,方法用於檢測數組全部元素是否都符合指定條件(經過函數提供),會檢測數組中的全部元素函數
every()是對數組中每一項運行給定函數,若是該函數對每一項返回true,則返回true。
1.若是數組中檢測到有一個元素不知足,則整個表達式返回 false ,且剩餘的元素不會再進行檢測。
2.若是全部元素都知足條件,則返回 true。code
some()是對數組中每一項運行給定函數,若是該函數對任一項返回true,則返回true。
1.若是有一個元素知足條件,則表達式返回true , 剩餘的元素不會再執行檢測。
2.若是沒有知足條件的元素,則返回false。it
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; })); // true console.log( arr.every( function( item, index, array ){ console.log( 'item=' + item + ',index='+index+',array='+array ); return item > 3; })); // false