find() method returns a value in the typed array, if an element satisfies the provided testing function. Otherwise undefined 被返回。 TypedArray is one of the typed array types here.

另请参阅 findIndex() method, which returns the index of a found element in the typed array instead of its value.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

typedarray.find(callback[, thisArg])
					

参数

callback
Function to execute on each value in the typed array, taking three arguments:
element

The current element being processed in the typed array.

index

The index of the current element being processed in the typed array.

array
The array find was called upon.
thisArg
Optional. Object to use as this when executing callback .

返回值

A value in the array if an element passes the test; otherwise, undefined .

描述

find method executes the callback function once for each element present in the typed array until it finds one where callback returns a true value. If such an element is found, find immediately returns the value of that element. Otherwise, find 返回 undefined . callback is invoked only for indexes of the typed array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

callback is invoked with three arguments: the value of the element, the index of the element, and the typed array object being traversed.

thisArg parameter is provided to find , it will be used as the this for each invocation of the callback . If it is not provided, then undefined 被使用。

find does not mutate the typed array on which it is called.

The range of elements processed by find is set before the first invocation of callback . Elements that are appended to the typed array after the call to find begins will not be visited by callback . If an existing, unvisited element of the typed array is changed by callback , its value passed to the visiting callback will be the value at the time that find visits that element's index; elements that are deleted are not visited.

范例

Find a prime number in a typed array

The following example finds an element in the typed array that is a prime number (or returns undefined if there is no prime number).

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, 5, 8, 12]);
console.log(uint8.find(isPrime)); // 5
								

规范

规范
ECMAScript (ECMA-262)
The definition of '%TypedArray%.prototype.find' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
find Chrome 45 Edge 14 Firefox 37 IE No Opera 32 Safari No WebView Android 45 Chrome Android 45 Firefox Android 37 Opera Android 32 Safari iOS No Samsung Internet Android 5.0 nodejs 4.0.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. TypedArray
  3. 特性
    1. TypedArray.BYTES_PER_ELEMENT
    2. TypedArray.name
    3. TypedArray.prototype.buffer
    4. TypedArray.prototype.byteLength
    5. TypedArray.prototype.byteOffset
    6. TypedArray.prototype.length
    7. get TypedArray[@@species]
  4. 方法
    1. TypedArray.from()
    2. TypedArray.of()
    3. TypedArray.prototype.copyWithin()
    4. TypedArray.prototype.entries()
    5. TypedArray.prototype.every()
    6. TypedArray.prototype.fill()
    7. TypedArray.prototype.filter()
    8. TypedArray.prototype.find()
    9. TypedArray.prototype.findIndex()
    10. TypedArray.prototype.forEach()
    11. TypedArray.prototype.includes()
    12. TypedArray.prototype.indexOf()
    13. TypedArray.prototype.join()
    14. TypedArray.prototype.keys()
    15. TypedArray.prototype.lastIndexOf()
    16. TypedArray.prototype.map()
    17. TypedArray.prototype.reduce()
    18. TypedArray.prototype.reduceRight()
    19. TypedArray.prototype.reverse()
    20. TypedArray.prototype.set()
    21. TypedArray.prototype.slice()
    22. TypedArray.prototype.some()
    23. TypedArray.prototype.sort()
    24. TypedArray.prototype.subarray()
    25. TypedArray.prototype.toLocaleString()
    26. TypedArray.prototype.toString()
    27. TypedArray.prototype.values()
    28. TypedArray.prototype[@@iterator]()
  5. 相关页面:
  6. Int8Array
  7. Uint8Array
  8. Uint8ClampedArray
  9. Int16Array
  10. Uint16Array
  11. Int32Array
  12. Uint32Array
  13. Float32Array
  14. Float64Array
  15. BigInt64Array
  16. BigUint64Array
  17. 继承:
  18. Function
  19. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  20. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  21. Object
  22. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  23. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1