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

Array.prototype.findLast() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.prototype.findLast()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2022 8.

Array findLast() . undefined .

In this article

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

const found = array1.findLast((element) => element > 45);

console.log(found);
// Expected output: 130

js
findLast(callbackFn)
findLast(callbackFn, thisArg)

callbackFn

. , . .

element

.

index

.

array

findLast() .

thisArg Optional

callbackFn this . .

( ) . undefined .

findlast() . callbackFn , callbackFn . findlast() . callbackFn , findlast() undefined . .

callbackFn . undefined .

find() . this length .

.

js
const inventory = [
  { name: "apples", quantity: 2 },
  { name: "bananas", quantity: 0 },
  { name: "fish", quantity: 1 },
  { name: "cherries", quantity: 5 },
];

// inventory   true .
function isNotEnough(item) {
  return item.quantity < 2;
}

console.log(inventory.findLast(isNotEnough));
// { name: "fish", quantity: 1 }

.

js
const inventory = [
  { name: "apples", quantity: 2 },
  { name: "bananas", quantity: 0 },
  { name: "fish", quantity: 1 },
  { name: "cherries", quantity: 5 },
];

const result = inventory.findLast(({ quantity }) => quantity < 2);

console.log(result);
// { name: "fish", quantity: 1 }

, undefined .

js
function isPrime(element) {
  if (element % 2 === 0 || element < 2) {
    return false;
  }
  for (let factor = 3; factor <= Math.sqrt(element); factor += 2) {
    if (element % factor === 0) {
      return false;
    }
  }
  return true;
}

console.log([4, 6, 8, 12].findLast(isPrime)); // undefined,  
console.log([4, 5, 7, 8, 9, 11, 12].findLast(isPrime)); // 11

callbackFn

array , . filter() findLast() .

js
const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6];
const lastTrough = numbers
  .filter((num) => num > 0)
  .findLast((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); // 2

findLast()

, undefined .

js
// 2, 3, 4     
const array = [0, 1, , , , 5, 6];

//       .
array.findLast((value, index) => {
  console.log(` : ${index} : ${value}`);
});
//  : 6 : 6
//  : 5 : 5
//  : 4 : undefined
//  : 3 : undefined
//  : 2 : undefined
//  : 1 : 1
//  : 0 : 0

//      .
array.findLast((value, index) => {
  //     5 
  if (index === 6) {
    console.log(` ${array[5]} array[5] `);
    delete array[5];
  }
  //  5   .
  console.log(` : ${index} : ${value}`);
});
//  5  array[5] 
//  : 6 : 6
//  : 5 : undefined
//  : 4 : undefined
//  : 3 : undefined
//  : 2 : undefined
//  : 1 : 1
//  : 0 : 0

findLast()

findlast() this length length .

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

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.findlast


Web Proxy Viewer  |  New URL  |  Original Page