W3cubDocs

/JavaScript

TypedArray.prototype.filter()

The filter() method of TypedArray instances creates a copy of a portion of a given typed array, filtered down to just the elements from the given typed array that pass the test implemented by the provided function. This method has the same algorithm as Array.prototype.filter().

Try it

Syntax

js
filter(callbackFn)
filter(callbackFn, thisArg)

Parameters

callbackFn

A function to execute for each element in the typed array. It should return a truthy value to keep the element in the resulting typed array, and a falsy value otherwise. The function is called with the following 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 typed array filter() was called upon.

thisArg Optional

A value to use as this when executing callbackFn. See iterative methods.

Return value

A copy of the given typed array containing just the elements that pass the test. If no elements pass the test, an empty typed array is returned.

Description

See Array.prototype.filter() for more details. This method is not generic and can only be called on typed array instances.

Examples

Filtering out all small values

The following example uses filter() to create a filtered typed array that has all elements with values less than 10 removed.

js
function isBigEnough(element, index, array) {
  return element >= 10;
}
new Uint8Array([12, 5, 8, 130, 44]).filter(isBigEnough);
// Uint8Array [ 12, 130, 44 ]

Specifications

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android Deno Node.js
filter 45 12 38 32 9.1 45 38 32 9.3 5.0 45 1.0 4.0.0

See also

© 2005–2023 MDN 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/filter