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.

entries()Array インスタンスのメソッドで、配列内の各要素に対するキー/値のペアを含む新しい配列イテレーターオブジェクトを返します。

試してみましょう

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

関連情報