确定指定的回调函数是否为数组中的任何元素均返回 true。
## 语法
~~~
array1.some(callbackfn[, thisArg])
~~~
## 参数
|参数|定义|
|--|--|
|array1|必需。一个数组对象。|
|callbackfn|必需。一个接受最多三个参数的函数。 some 方法会为 array1 中的每个元素调用 callbackfn 函数,直到 callbackfn 返回 true,或直到到达数组的结尾。|
|thisArg|可选。可在 callbackfn 函数中为其引用 this 关键字的对象。如果省略 thisArg,则 undefined 将用作 this 值。|
## 返回值
如果 callbackfn 函数为任何数组元素均返回 true,则为 true;否则为 false。
## 异常
如果 callbackfn 参数不是函数对象,则将引发 TypeError 异常。
~~~
Exception Condition
~~~
## 备注
some 方法会按升序索引顺序对每个数组元素调用 callbackfn 函数,直到 callbackfn 函数返回 true。如果找到导致 callbackfn 返回 true 的元素,则 some 方法会立即返回 true。如果回调不对任何元素返回 true,则 some 方法会返回 false。
不为数组中缺少的元素调用该回调函数。
除了数组对象之外,some 方法可由具有 length 属性且具有已按数字编制索引的属性名的任何对象使用。
> 注意:可以使用 every 方法 (Array) (JavaScript)检查回调函数是否对数组的所有元素都返回 true。
### 回调函数语法
回调函数的语法如下所示:
function callbackfn(value, index, array1)
可使用最多三个参数来声明回调函数。
下表列出了回调函数参数。
|回调参数|定义|
|--|--|
|Value|数组元素的值。|
|index|数组元素的数字索引。|
|array1|包含该元素的数组对象。|
### 修改数组对象
数组对象可由回调函数修改。
下表描述了在 some 方法启动后修改数组对象所获得的结果。
|some 方法启动后的条件|元素是否传递给回调函数|
|--|--|
|在数组的原始长度之外添加元素。|否。|
|添加元素以填充数组中缺少的元素。|是,如果该索引尚未传递给回调函数。|
|元素被更改。|是,如果该元素尚未传递给回调函数。|
|从数组中删除元素。|否,除非该元素已传递给回调函数。|
下面的示例使用 some 方法查明数组中的任何元素是否相等。
~~~
// The callback function.
function CheckIfEven(value, index, ar) {
if (value % 2 == 0)
return true;
}
var numbers = [1, 15, 4, 10, 11, 22];
var evens = numbers.some(CheckIfEven);
document.write(evens);
// Output:
// true
~~~
下面的示例演示如何使用 thisArg 参数,该参数指定 this 关键字可引用的对象。它检查数组中是否有数字位于传递的对象所提供的范围之外。
~~~
// Create a function that returns true if the value is
// outside the range.
var isOutsideRange = function (value) {
return value < this.minimum || value > this.maximum;
}
// Create an array of numbers.
var numbers = [6, 12, 16, 22, -12];
// The range object is to be the 'this' object.
var range = { minimum: 10, maximum: 20 };
document.write(numbers.some(isOutsideRange, range));
// Output: true
~~~
- ActiveXObject对象
- Array对象
- constructor属性
- length属性
- prototype属性
- Array.from函数
- Array.isArray函数
- Array.of函数
- concat方法
- entries方法
- every方法
- fill方法
- filter方法
- findIndex方法
- forEach方法
- indexOf方法
- join方法
- keys方法
- lastIndexOf方法
- map方法
- pop方法
- push方法
- reduce方法
- reduceRight方法
- reverse方法
- shift方法
- slice方法
- some方法
- sort方法
- splice方法
- toString方法
- unshift方法
- valueOf方法
- values方法
- ArrayBuffer对象
- byteLength属性
- ArrayBuffer.isView函数
- slice方法
- arguments对象
- 0...n 属性(参数)
- callee 属性(参数)
- length 属性 (arguments)
- Boolean对象
- constructor 属性(布尔值)
- prototype 属性(布尔值)
- toString 方法 (Boolean)
- valueOf 方法 (Boolean)
- DataView对象
- buffer属性 (DataView)
- byteLength属性(DataView)
- byteOffset属性(DataView)
- getInt8方法(DataView)
- getUint8方法(DataView)
- getInt16方法(DataView)
- getUint16方法(DataView)
- getInt32方法(DataView)
- getUint32方法(DataView)
- getFloat32方法(DataView)
- getFloat64方法(DataView)
- setInt8方法(DataView)
- setUint8方法(DataView)
- setInt16方法(DataView)
- setUint16方法(DataView)
- setInt32方法(DataView)
- setUint32方法(DataView)
- setFloat32方法(DataView)
- setFloat64方法(DataView)
- Date对象
- Debug对象
- Enumerator对象
- Error对象
- Float32Array对象
- Float64Array对象
- Function对象
- Global对象
- Int8Array对象
- Int16Array对象
- Int32Array对象
- Intl.Collator对象
- Intl.DateTimeFormat对象
- Intl.NumberFormat对象
- JSON对象
- Map对象
- Math对象
- Number对象
- Object对象
- Promise对象
- 代理对象
- Reflect对象
- RegExp对象
- 正则表达式对象
- Set对象
- String对象
- 符号对象
- Uint8Array对象
- Uint8ClampedArray对象
- Uint16Array对象
- Uint32Array对象
- VBArray对象
- WeakMap对象
- WeakSet对象
- WinRTError对象