• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • TypedArray.prototype.findIndex()

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

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

    语法

    typedarray.findIndex(callback[, thisArg])
    

    参数

    callback
    用于在类型化数组中的每个元素上执行的函数,接受三个参数:
    element
    要处理的类型化数组的当前元素。
    index
    要处理的当前元素在类型化数组中的下标
    array
    find在其上调用的类型化数组
    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).

    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, 未发现
    console.log(uint16.findIndex(isPrime)); // 2
    

    Polyfill

    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') {
                    // 尝试将其转换为函数
                    if ( ! (evaluator = eval(evaluator)) ){
                        throw new TypeError();
                    }
                } else {
                    throw new TypeError();
                }
            }
            
            var i;
            if (thisArg === undefined) {  // 为 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;
    };