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;
}));
运行结果:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。