every()与some()办法都是JS中数组的迭代办法。
1. 区别
every()
判断数组中是否每个元素都满足条件
只有全副都满足条件才返回true;
只有有一个不满足就返回false;
some()
判断数组中是否至多有一个元素满足条件
只有有一个满足就返回true
只有都不满足时才返回false
1. 用法
every()
var arr = [ 1, 2, 3, 4, 5, 6 ]; console.log( arr.every( function( item, index, array ){ console.log( 'item=' + item + ',index='+index+',array='+array ); return item > 3; }));
运行后果:
因为第一个数小于3所以间接return进来了。
some()
var arr = [ 1, 2, 3, 4, 5, 6 ]; console.log( arr.every( function( item, index, array ){ console.log( 'item=' + item + ',index='+index+',array='+array ); return item > 3; }));
运行后果: