TypedArray.prototype.findLast()

Baseline 2022

Newly available

Since August 2022, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

TypedArray 인스턴스의 findLast() 메서드는 형식화 배열을 역순으로 순회하며 제공된 테스트 함수를 만족하는 첫 번째 요소의 값을 반환합니다. 테스트 함수를 만족하는 요소가 없으면 undefined가 반환됩니다. 이 메서드는 Array.prototype.findLast()와 같은 알고리즘을 가집니다.

시도해보기

구문

js
findLast(callbackFn)
findLast(callbackFn, thisArg)

매개변수

callbackFn

형식화 배열의 각 요소에 대해 실행할 함수입니다. 일치하는 요소를 찾았으면 참 같은 값을 반환하고, 그렇지 않으면 거짓 같은 값을 반환해야 합니다. 함수는 다음 인수를 사용하여 호출됩니다.

element

형식화 배열에서 현재 처리되고 있는 요소.

index

형식화 배열에서 현재 처리되고 있는 요소의 인덱스.

array

findLast()가 호출된 형식화 배열.

thisArg Optional

callbackFn을 실행할 때 this로 사용할 값입니다. 순회 메서드를 참고하세요.

반환 값

제공된 테스트 함수를 만족하는 형식화 배열의 마지막(가장 높은 인덱스) 요소 값. 일치하는 요소를 찾을 수 없으면 undefined를 반환합니다.

설명

보다 자세한 설명은 Array.prototype.findLast()을 참고하시기 바랍니다. 이 메서드는 범용 메서드가 아니며 오직 형식화 배열 인스턴스에서만 호출할 수 있습니다.

예제

형식화 배열에서 마지막 소수 찾기

다음 예제는 형식화 배열의 마지막 요소 중 소수인 요소를 반환하거나, 소수가 없는 경우 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;
}

let uint8 = new Uint8Array([4, 6, 8, 12]);
console.log(uint8.findLast(isPrime)); // undefined (배열 내 소수 없음)
uint8 = new Uint8Array([4, 5, 7, 8, 9, 11, 12]);
console.log(uint8.findLast(isPrime)); // 11

콜백이 모든 요소를 방문했으며 변경되었을 수 있습니다

다음 예제는 모든 요소가 방문되었으며 콜백에 전달된 값이 방문 시 해당 요소의 값임을 보여줍니다.

js
// 인덱스 2,3,4에는 요소가 없는 배열을 선언합니다
// 누락된 요소는 0으로 초기화 됩니다.
const uint8 = new Uint8Array([0, 1, , , , 5, 6]);

// 역순으로 요소를 순회합니다.
// 모든 요소를 방문하는 점을 유의하시기 바랍니다.
uint8.findLast((value, index) => {
  console.log(`Visited index ${index} with value ${value}`);
});

// 삭제된 것을 포함한 모든 인덱스를 보여줍니다
uint8.findLast((value, index) => {
  // 첫 순회때 요소 3을 수정합니다
  if (index === 6) {
    console.log("Set uint8[3] to 44");
    uint8[3] = 44;
  }
  // 요소 3은 여전히 방문하지만 새로운 값을 가집니다
  console.log(`Visited index ${index} with value ${value}`);
});
// Visited index 6 with value 6
// Visited index 5 with value 5
// Visited index 4 with value 0
// Visited index 3 with value 0
// Visited index 2 with value 0
// Visited index 1 with value 1
// Visited index 0 with value 0
// Set uint8[3] to 44
// Visited index 6 with value 6
// Visited index 5 with value 5
// Visited index 4 with value 0
// Visited index 3 with value 44
// Visited index 2 with value 0
// Visited index 1 with value 1
// Visited index 0 with value 0

명세서

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.findlast

브라우저 호환성

BCD tables only load in the browser

같이 보기