Array.prototype.entries()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since May 2018.

Array 인스턴스의 entries() 메서드는 배열의 각 인덱스에 대한 키/값 쌍을 포함하는 새 배열 반복자 객체를 반환합니다.

시도해보기

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

const iterator1 = array1.entries();

console.log(iterator1.next().value);
// Expected output: Array [0, "a"]

console.log(iterator1.next().value);
// Expected output: Array [1, "b"]

구문

js
entries()

매개변수

없음.

반환 값

설명

희소 배열이 사용되는 경우, entries() 메서드는 빈 슬롯에 undefined값이 있는것 처럼 순회합니다.

entries() 메서드는 범용입니다. this 값에는 length 속성과 정수 키 속성만 있을 것으로 예상합니다.

예시

인덱스와 요소 순회하기

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

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

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

for...of 루프 사용하기

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

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

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

희소 배열 순회하기

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

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

entries()를 배열이 아닌 객체에서 사용하기

entries() 메서드는 thislength 속성을 읽은 다음 키가 length보다 작은 음수가 아닌 정수 속성에 각각 접근합니다.

js
const arrayLike = {
  length: 3,
  0: "a",
  1: "b",
  2: "c",
  3: "d", // length가 3이므로 entries()에서 무시됩니다.
};
for (const entry of Array.prototype.entries.call(arrayLike)) {
  console.log(entry);
}
// [ 0, 'a' ]
// [ 1, 'b' ]
// [ 2, 'c' ]

명세서

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

브라우저 호환성

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
entries

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

같이 보기