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
element
The current element being processed in the typed array.
index
The index of the current element being processed in the typed array.
array
find
was called upon.
thisArg
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.
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. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
TypedArray
TypedArray.from()
TypedArray.of()
TypedArray.prototype.copyWithin()
TypedArray.prototype.entries()
TypedArray.prototype.every()
TypedArray.prototype.fill()
TypedArray.prototype.filter()
TypedArray.prototype.find()
TypedArray.prototype.findIndex()
TypedArray.prototype.forEach()
TypedArray.prototype.includes()
TypedArray.prototype.indexOf()
TypedArray.prototype.join()
TypedArray.prototype.keys()
TypedArray.prototype.lastIndexOf()
TypedArray.prototype.map()
TypedArray.prototype.reduce()
TypedArray.prototype.reduceRight()
TypedArray.prototype.reverse()
TypedArray.prototype.set()
TypedArray.prototype.slice()
TypedArray.prototype.some()
TypedArray.prototype.sort()
TypedArray.prototype.subarray()
TypedArray.prototype.toLocaleString()
TypedArray.prototype.toString()
TypedArray.prototype.values()
TypedArray.prototype[@@iterator]()
Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
BigInt64Array
BigUint64Array
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()