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.
시도해보기
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()
매개변수
없음.
반환 값
새 순회 가능 반복자 객체.
설명
예시
인덱스와 요소 순회하기
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()
메서드는 this
의 length
속성을 읽은 다음 키가 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 |
브라우저 호환성
BCD tables only load in the browser