Javascript Uint8ClampedArray every()

Introduction

The Javascript Uint8ClampedArray every() method tests whether all elements in the typed array pass the provided function.

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

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




Required




Function to test for each element
Taking three arguments:
currentValue - The current element being processed.
index - the index of the current element.
array - typed array itself.
thisArg
Optional.
Value to use as this when executing callback.

It returns true if the callback function returns a truthy value for every array element; otherwise, false.

every() does not mutate the typed array on which it is called.

The following example tests whether all elements in the typed array are bigger than 10.

function isBigEnough(element, index, array) {
  return element >= 10;
}
let a = new Uint8ClampedArray([12, 5, 8, 130, 44]).every(isBigEnough);   // false
console.log(a);/*  w  ww .j av a 2 s. c  o  m*/
a = new Uint8ClampedArray([12, 54, 18, 130, 44]).every(isBigEnough); // true
console.log(a);

Testing 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]).every(elem => elem >= 10); // false
console.log(a);
new Uint8ClampedArray([12, 54, 18, 130, 44]).every(elem => elem >= 10); // true
console.log(a);



PreviousNext

Related