pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/entries

ipt>

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

Array.prototype.entries()

Baseline 広く利用可能

この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。

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

試してみましょう

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

const iterator = array.entries();

console.log(iterator.next().value);
// 予想される結果: Array [0, "a"]

console.log(iterator.next().value);
// 予想される結果: 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' ]

仕様書

仕様書
ECMAScript® 2027 Language Specification
# sec-array.prototype.entries

ブラウザーの互換性

関連情報

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy