检查数组元素

2016-11-14  本文已影响6人  Silence_wh

1.Array.prototype.every()

方法定义:

The every() method tests whether all elements in the array pass the test implemented by the provided function.

语法:

arr.every(callback[,thisArg])

示例:

function isBigEnough(item, index, array){
return item > 10;
}
[12,5,34,65].every(isBigEnough); // false
[50,25,14,35].every(isBigEnough); // true

2.Array.prototype.some()

方法定义:

The some() method tests whether some element in the array passes the test implemented by the provided function.

语法:

arr.some(callback[,thisArg])

示例:

function isBiggerThan10(item, index, array){
return item > 10;
}
[5,6,8,3].some(isBiggerThan10); // false
[12,5,8,1].some(isBiggerThan10); // true

总结:

第一次在简书发表文章,如有错误或建议,还请大家在评论区指出。

上一篇 下一篇

猜你喜欢

热点阅读