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

typedArray.findIndex

如果某个元素满足所提供的测试函数,findIndex()方法返回类型化数组中的下标。否则返回 -1。TypedArray是这里的类型化数组类型之一。

同时请参见find()方法,它返回了类型化数组中所发现元素的,而不是它的下标。

语法

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

参数

callback用于在类型化数组中的每个元素上执行的函数,接受三个参数:element要处理的类型化数组的当前元素。index要处理的当前元素在类型化数组中的下标arrayfind在其上调用的类型化数组thisArg可选,执行callback时的this值。

返回值

如果元素通过了测试,则为数组下标,否则为 -1。

描述

findIndex方法对类型化数组中的每个元素执行一次?callback函数,直到它找到一个使?callback返回?true的元素。如果发现了一个这样的元素,find方法将会立即返回该元素的下标。否则,findIndex方法会返回 -1。callback只会对那些已经被赋值的索引调用。不会对那些被删除或从来没被赋值的索引调用。

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

如果将thisArg参数提供给findIndex,它会在调用时传递给callback,作为它的this值。如果没有提供,会使用undefined

findIndex不修改在其上调用的类型化数组。

findIndex处理的元素范围在callback调用之前就确定了。 在findIndex调用之后添加到数组的元素不会由callback访问。 如果类型化数组的现有元素被改变,或被删除,它们传给callback的值是findIndex访问它们时候的值。已删除的元素不会被访问。

示例

在类型化数组中寻找质数的下标

下面的示例在类型化数组中寻找质数元素的下标(如果没有质数则返回 -1).

代码语言:javascript
复制
function isPrime(element, index, array) {
  var start = 2;
  while (start <= Math.sqrt(element)) {
    if (element % start++ < 1) {
      return false;
    }
  }
  return element > 1;
}

var uint8 = new Uint8Array([4, 6, 8, 12]);
var uint16 = new Uint16Array([4, 6, 7, 12]);

console.log(uint8.findIndex(isPrime)); // -1, not found
console.log(uint16.findIndex(isPrime)); // 2

Polyfill

代码语言:javascript
复制
TypedArray.prototype.findIndex = Array.prototype.findIndex = Array.prototype.findIndex || function(evaluator, thisArg) {
????????'use strict';
????????if (!this) {
??????????throw new TypeError('Array.prototype.some called on null or undefined');
????????}
????
????????if (typeof(evaluator) !== 'function') {
????????????if (typeof(evaluator) === 'string') {
????????????????// Attempt to convert it to a function
????????????????if ( ! (evaluator = eval(evaluator)) ){
????????????????????throw new TypeError();
????????????????}
????????????} else {
????????????????throw new TypeError();
????????????}
????????}
????????
????????var i;
????????if (thisArg === undefined) { ?// Optimize for thisArg
????????????for (i in this) {
????????????????if (evaluator(this[i], i, this)) {
????????????????????return i;
?????????? ?????}
???????? ???}
????????????return -1;
???? ???}
????????for (i in this) {
????????? ??if (evaluator.call(thisArg, this[i], i, this)) {
??????????? ??? return i;
?????????? ?}
????????}
????????return -1;
};

规范

Specification

Status

Comment

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

Standard

Initial definition.

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

Draft

?

浏览器兼容性

Feature

Chrome

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

No support

37.0 (37.0)

No support

No support

No support

Feature

Android

Chrome for Android

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

No support

No support

37.0 (37.0)

No support

No support

No support

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com