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().
function isNegative(element, index, array) {
return element < 0;
}
const int8 = new Int8Array([-10, 20, -30, 40, -50]);
const negInt8 = int8.filter(isNegative);
console.log(negInt8);
// Expected output: Int8Array [-10, -30, -50]filter(callbackFn)
filter(callbackFn, thisArg)callbackFnA 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:
elementThe current element being processed in the typed array.
indexThe index of the current element being processed in the typed array.
arrayThe typed array filter() was called upon.
thisArg OptionalA value to use as this when executing callbackFn. See iterative methods.
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.
See Array.prototype.filter() for more details. This method is not generic and can only be called on typed array instances.
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 ]