Javascript Uint8ClampedArray find()

Introduction

The Javascript Uint8ClampedArray find() method searches a value in the typed array via the provided testing function.

If not found, undefined is returned.

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




Required




Function to execute on each value
Taking three arguments:
element - the current element being processed.
index - the index of the current element.
array - the array itself.
thisArg
Optional.
Object to use as this when executing callback.

Find a prime number in a typed array

function isPrime(element, index, array) {
   var start = 2;
   while (start <= Math.sqrt(element)) {
       if (element % start++ < 1) {
           return false;
       }/* w w w  . j a v a2  s.c om*/
   }
   return element > 1;
}

var uint8 = new Uint8ClampedArray([4, 5, 8, 12]);
console.log(uint8.find(isPrime)); // 5



PreviousNext

Related