首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

typedArray.some

some()方法测试类型数组中的某个元素是否通过了由提供的函数实现的测试。这个方法和Array.prototype.some()类似 TypedArray是这里的类型数组类型之一。

语法

代码语言:javascript
复制
typedarray.some(callback[, thisArg])

参数

callback函数为每个元素测试,取三个参数:currentValue正在处理的类型数组中的当前元素。index在类型数组中处理当前元素的索引。被调用的array数组every被调用。thisArg可选的。this执行时使用的值callback

返回值

如果回调函数返回任何数组元素的值则返回true; 否则,返回false

描述

some方法callback为类型数组中的每个元素执行一次函数,直到找到一个callback返回真值的函数。如果找到这样的元素,some立即返回true。否则,some返回false

callback 被调用三个参数:元素的值,元素的索引和被遍历的数组对象。

如果thisArg提供了一个参数some,它将callback在被调用时传递给它,作为它的this值。否则,该值undefined将被传递以用作其this值。this最终可观察到的值callback是根据用于确定this函数所看到的通常规则来确定的。

some 不会改变它被调用的类型数组。

示例

测试所有类型数组元素的大小

以下示例测试类型数组中的任何元素是否大于10。

代码语言:javascript
复制
function isBiggerThan10(element, index, array) {
  return element > 10;
}
new Uint8Array([2, 5, 8, 1, 4]).some(isBiggerThan10); // false
new Uint8Array([12, 5, 8, 1, 4]).some(isBiggerThan10); // true

使用箭头函数测试键入的数组元素

箭头函数为同一个测试提供了一个更短的语法。

代码语言:javascript
复制
new Uint8Array([2, 5, 8, 1, 4]).some(elem => elem > 10); // false
new Uint8Array([12, 5, 8, 1, 4]).some(elem => elem > 10); // true

Polyfill

由于没有名称为TypedArray的全局对象,因此必须在“根据需要”的基础上进行填充

代码语言:javascript
复制
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.some
if (!Uint8Array.prototype.some) {
  Object.defineProperty(Uint8Array.prototype, 'some', {
    value: Array.prototype.some
  });
}

如果您需要支持真正过时的不支持的JavaScript引擎,Object.defineProperty最好不要填充Array.prototype方法,因为您无法使它们不可枚举。

规范

Specification

Status

Comment

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray.prototype.some' in that specification.

Standard

Initial definition.

ECMAScript 2017 Draft (ECMA-262)The definition of 'TypedArray.prototype.some' in that specification.

Draft

浏览器兼容性

Feature

Chrome

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

45

37 (37)

No support

32

10

Feature

Android

Chrome for Android

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

No support

No support

37 (37)

No support

No support

10

扫码关注腾讯云开发者

领取腾讯云代金券