此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
TypedArray.prototype.some()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016年9月.
这个some() 方法检测TypedArray 的一些元素是否通过所提供函数的测试。这个方法和Array.prototype.some()相同。TypedArray 是typed array types 之一。
In this article
尝试一下
function isNegative(element, index, array) { return element < 0;}const int8 = new Int8Array([-10, 20, -30, 40, -50]);const positives = new Int8Array([10, 20, 30, 40, 50]);console.log(int8.some(isNegative));// Expected output: trueconsole.log(positives.some(isNegative));// Expected output: false语法
some(callbackFn)some(callbackFn, thisArg)参数
callback一个测试每个元素的函数,有 3 个参数:
currentValue在 typed array 中,正在被测试的元素。
index在 typed array 中,正在被测试元素的索引。
array正在被调用的 typed array 本身。
thisArg可选的。
callback回调函数的this值 .
返回值
true 如果 callback 函数以任一数组元素为参数调用时,返回truthy; 否则,false.
描述
对于 typed array 中的每个元素,some方法执行一次callback,直到找到一个callback 返回 true 的元素。如果一个元素被找到,some 立即返回true. 否则,some 返回false.
callback 期望 3 个参数:元素的值,元素的索引,和被遍历的数组对象。
如果some 提供thisArg, 那么thisArg会作为callback 调用时的this 值. 否则,callback 调用时的this 是undefined.callback 最终可观测的this 是根据确定函数 this 的通常规则 所确定的。
some 被调用不会改变 typed array .
示例
>测试类型化数组所有元素的大小
以下示例测试 typed array 中的所有元素都大于 10.
function isBiggerThan10(element, index, array) { return element > 10;}new Uint8Array([2, 5, 8, 1, 4]).some(isBiggerThan10); // falsenew Uint8Array([12, 5, 8, 1, 4]).some(isBiggerThan10); // true规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-%typedarray%.prototype.some> |