TypedArray.prototype.findIndex() - JavaScript TypedArray 对象
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 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; };
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!