JS数组中every()与some()两者迭代方法
2017-06-20 14:05
525 查看
every()与some()方法都是JS中数组的迭代方法。every()是对数组中的每一项运行给定函数,如果该函数对每一项返回true,则返回true。some()是对数组中每一项运行指定函数,如果该函数对任一项返回true,则返回true。
我其实在看完这两个函数的介绍时,心里就在想,这两个会不会像逻辑运算符||和&&一样是短路操作?
运行如下代码便知分晓:
[javascript] view
plain copy
<span style="font-family:Courier New;font-size:14px;"><script type="text/javascript">
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;
}));
</script></span>
运行结果如下:
可以看到,some方法是碰到一个返回true的值时候就返回了,并没有继续往下运行,而every也一样,第一个值就是一个false,所以后面也没有进行下去的必要了,就直接返回结果了。
我其实在看完这两个函数的介绍时,心里就在想,这两个会不会像逻辑运算符||和&&一样是短路操作?
运行如下代码便知分晓:
[javascript] view
plain copy
<span style="font-family:Courier New;font-size:14px;"><script type="text/javascript">
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;
}));
</script></span>
运行结果如下:
可以看到,some方法是碰到一个返回true的值时候就返回了,并没有继续往下运行,而every也一样,第一个值就是一个false,所以后面也没有进行下去的必要了,就直接返回结果了。
相关文章推荐
- 关于JS数组中every与some两者迭代方法探索
- 一张图看懂JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some
- 一张图看懂JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some
- [置顶] JAVASCRIPT中数组的迭代方法:FOREACH、MAP、FILTER、REDUCE、EVERY、SOME各个说明
- JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some
- Js数组的map,filter,reduce,every,some方法
- 一张图看懂JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some
- js数组的迭代器方法(some、every、forEach、map、filter)
- Js 数组方法:filter()、map()、some()、every()、forEach()、indexOf()、lastIndexOf()、
- 一张图看懂JavaScript中数组的迭代方法:forEach、map、filter、reduce、every、some
- js数组的迭代器方法(some、every、forEach、map、filter)
- 数组的5中迭代方法 filter()、map()、forEach()、every()、 some()
- JS数组方法总结shift()、unshift()、pop()、push()、concat()、splice()、filter()、map()、some()、every()、forEach()等方法
- 关于数组中迭代的方法:every(),fileter(),forEach(),map(),some()
- JS数组中every(),filter(),forEach(),map(),some()方法学习笔记!
- js数组的遍历方法filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()
- 重写数组迭代新方法every,filter,map,some,forEach
- js中数组过滤、遍历、迭代every、some、filter、map、forEach、reduce、reduceRight
- js数组的迭代器方法(some、every、forEach、map、filter)
- 数组的迭代方法(every、filter、forEach、map、some)