Javascript Int16Array every()

Introduction

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

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

Int16Array.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 Int16Array([12, 5, 8, 130, 44]).every(isBigEnough);   // false
console.log(a);//from  ww w  . ja va2  s .  c o m
a = new Int16Array([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 Int16Array([12, 5, 8, 130, 44]).every(elem => elem >= 10); // false
console.log(a);
new Int16Array([12, 54, 18, 130, 44]).every(elem => elem >= 10); // true
console.log(a);



PreviousNext

Related