[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/findIndex [Back]  [Original]

TypedArray.prototype.findIndex() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

TypedArray.prototype.findIndex()

Baseline

20169

findIndex() TypedArray -1 Array.prototype.findIndex()

function isNegative(element, index, array) {
  return element < 0;
}

const int8 = new Int8Array([10, -20, 30, -40, 50]);

console.log(int8.findIndex(isNegative));
// : 1

js
findIndex(callbackFn)
findIndex(callbackFn, thisArg)

callbackFn

element

index

array

findIndex()

thisArg

callbackFn this

-1

Array.prototype.findIndex()

-1

js
function isPrime(n) {
  if (n < 2) {
    return false;
  }
  if (n % 2 === 0) {
    return n === 2;
  }
  for (let factor = 3; factor * factor <= n; factor += 2) {
    if (n % factor === 0) {
      return false;
    }
  }
  return true;
}

const uint8 = new Uint8Array([4, 6, 8, 12]);
const uint16 = new Uint16Array([4, 6, 7, 12]);

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

: isPrime()

ECMAScript 2027 LanguageSpecification
# sec-%typedarray%.prototype.findindex


Web Proxy Viewer  |  New URL  |  Original Page