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 |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
entries |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.