Array.prototype.entries()

entries() 메서드는 배열의 각 인덱스에 대한 키/값 쌍을 가지는 새로운 Array Iterator 객체를 반환합니다.

시도해보기

구문

entries()

반환 값

Array 반복자 인스턴스 객체.

설명

sparse arrays와 사용하면 entries() 메서드가 빈 슬롯을 undefined 가 있는 것처럼 순회합니다.

예시

인덱스와 요소 순회하기

const a = ["a", "b", "c"];

for (const [index, element] of a.entries()) {
  console.log(index, element);
}

// 0 'a'
// 1 'b'
// 2 'c'

for…of 루프 사용

const array = ["a", "b", "c"];
const arrayEntries = array.entries();

for (const element of arrayEntries) {
  console.log(element);
}

// [0, 'a']
// [1, 'b']
// [2, 'c']

sparse arrays 순회하기

entires() 는 빈 슬롯을 undefined인 것처럼 접근합니다.

for (const element of [, "a"].entries()) {
  console.log(element);
}
// [0, undefined]
// [1, 'a']

명세서

Specification
ECMAScript Language Specification
# sec-array.prototype.entries

브라우저 호환성

BCD tables only load in the browser

같이 보기