Javascript Uint32Array findIndex()

Introduction

The Javascript Uint32Array findIndex() method searches an index in the typed array by the provided testing function.

-1 is returned if not found.

Uint32Array.findIndex(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 typed array itself.
thisArg
Optional
Object to use as this when executing callback.

Find the index of a prime number in a typed array

function isPrime(element, index, array) {
  var start = 2;//from  w ww .j  a  v  a2s.  c o  m
  while (start <= Math.sqrt(element)) {
    if (element % start++ < 1) {
      return false;
    }
  }
  return element > 1;
}

var uint8 = new Uint32Array([4, 6, 8, 12]);
console.log(uint8);
var uint16 = new Uint32Array([4, 6, 7, 12]);
console.log(uint16);

console.log(uint8.findIndex(isPrime)); // -1, not found
console.log(uint16.findIndex(isPrime)); // 2



PreviousNext

Related