The find()
method returns a value in the typed array, if an element satisfies the provided testing function. Otherwise undefined
is returned. TypedArray is one of the typed array types here.
See also the findIndex()
method, which returns the index of a found element in the typed array instead of its value.
typedarray.find(callback[, thisArg])
callback
element
index
array
find
was called upon.thisArg
this
when executing callback
.A value in the array if an element passes the test; otherwise, undefined
.
The 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
returns 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.
If a 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
is used.
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
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of '%TypedArray%.prototype.find' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of '%TypedArray%.prototype.find' in that specification. | Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | No | ? | 37 | No | No | No |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | No | No | ? | 37 | No | No | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/find