Javascript Uint8ClampedArray filter()

Introduction

The Javascript Uint8ClampedArray filter() method filters the typed array by the provided function.

This method works the same as Array.prototype.filter().

Uint8ClampedArray.filter(callback[, thisArg])
Parameter
Optional
Meaning
callback


Required


Function to test each element of the typed array.
Taking three arguments (element, index, Uint8ClampedArray).
Return true to keep the element, false otherwise.
thisArg
Optional
Value to use as this when executing callback.

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;
}
let a = new Uint8ClampedArray([12, 5, 8, 130, 44]).filter(isBigEnough); 
console.log(a);/*from   w  w w .jav  a 2  s .c  o m*/

Filtering typed array elements using arrow functions

Arrow functions provide a shorter syntax for the same test.

let a = new Uint8ClampedArray([12, 5, 8, 130, 44]).filter(elem => elem >= 10); 
console.log(a);



PreviousNext

Related