语法
array.some(function(currentValue,index,arr),thisValue)
currentValue
:必选。当前元素。index
:可选。当前元素索引值。arr
:可选。当前元素所属的数组对象。thisValue
:可选。传递给函数,用作“this”的值;如果省略,"this"值则为"undefined"
参数分解:
const arr = [3,4,5,6,7,8]
arr.some(function (currentValue,index,arr) {
console.log(currentValue);
})
复制代码
const arr = [3,4,5,6,7,8]
arr.some(function (currentValue,index,arr) {
console.log(index);
})
复制代码
const arr = [3,4,5,6,7,8]
arr.some(function (currentValue,index,arr) {
console.log(arr);
})
复制代码
用途
遍历数组中每个元素,判断其是否满足指定函数的指定条件,返回true或者false
- 如果一个元素满足条件,返回true,且后面的元素不再被检测
- 所有元素都不满足条件,则返回false
- 不会改变原始数组
- 不会对空数组进行检测;数组为空的话,直接返回false
实例
检测数组中是否有值满足函数指定的条件
const arr = [4, 12, 16, 20];
arr.some(item => {
item > 18
})
//true
复制代码