Javascript BigInt64Array findIndex()

Introduction

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

-1 is returned if not found.

BigInt64Array.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;/*w w w .j  a  va 2 s  . co m*/
  while (start <= Math.sqrt(element)) {
    if (element % start++ < 1) {
      return false;
    }
  }
  return element > 1;
}

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

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



PreviousNext

Related