Array.prototype.entries()
entries()
メソッドは、配列内の各要素に対するキー/値のペアを含む新しい Array イテレーターオブジェクトを返します。
試してみましょう
構文
entries();
返値
新しい Array
イテレーターオブジェクトを返します。
例
添字と要素の反復処理
const a = ["a", "b", "c"];
for (const [index, element] of a.entries()) {
console.log(index, element);
}
// 0 'a'
// 1 'b'
// 2 'c'
for...of ループの使用
const array = ["a", "b", "c"];
const arrayEntries = array.entries();
for (const element of arrayEntries) {
console.log(element);
}
// [0, 'a']
// [1, 'b']
// [2, 'c']
仕様書
Specification |
---|
ECMAScript Language Specification # sec-array.prototype.entries |
ブラウザーの互換性
BCD tables only load in the browser