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

Array.prototype.findLastIndex() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.findLastIndex()

Baseline

20228

findLastIndex() Array -1

findLast()

const array = [5, 12, 50, 130, 44];

const isLargeNumber = (element) => element > 45;

console.log(array.findLastIndex(isLargeNumber));
// : 3
// Index of element with value: 130

js
findLastIndex(callbackFn)
findLastIndex(callbackFn, thisArg)

callbackFn

element

index

array

findLastIndex()

thisArg

callbackFn this

-1

findLastIndex() callbackFn callbackFn findLastIndex() callbackFn findLastIndex() -1

callbackFn undefined

findLastIndex() this length

-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;
}

console.log([4, 6, 8, 12].findLastIndex(isPrime)); // -1, not found
console.log([4, 5, 7, 8, 9, 11, 12].findLastIndex(isPrime)); // 5

: isPrime()

callbackFn 3

array filter() findLastIndex()

js
const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6];
const lastTrough = numbers
  .filter((num) => num > 0)
  .findLastIndex((num, idx, arr) => {
    // arr 
    // 
    if (idx > 0 && num >= arr[idx - 1]) return false;
    if (idx < arr.length - 1 && num >= arr[idx + 1]) return false;
    return true;
  });
console.log(lastTrough); // 6

findLastIndex()

undefined

js
console.log([1, , 3].findLastIndex((x) => x === undefined)); // 1

findLastIndex()

findLastIndex() this length length

js
const arrayLike = {
  length: 3,
  0: 2,
  1: 7.3,
  2: 4,
  3: 3, // length  3  findLastIndex() 
};
console.log(
  Array.prototype.findLastIndex.call(arrayLike, (x) => Number.isInteger(x)),
); // 2

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.findlastindex


Web Proxy Viewer  |  New URL  |  Original Page