UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ TypedArray/ 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.

Syntax

filter(callbackFn)
filter(callbackFn, thisArg)

Parameters

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.

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

Specifications

Browser compatibility

See also