NodeList: entries() メソッド
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2017年10月以降、すべてのブラウザーで利用可能です。
NodeList.entries() メソッドは、オブジェクト内にあるすべてのキーと値のペアを走査することができるイテレーターを返します。値は Node オブジェクトです。
構文
js
entries()
返値
イテレーターを返します。
例
js
const node = document.createElement("div");
const kid1 = document.createElement("p");
const kid2 = document.createTextNode("hey");
const kid3 = document.createElement("span");
node.appendChild(kid1);
node.appendChild(kid2);
node.appendChild(kid3);
const list = node.childNodes;
// for...of を使用
for (const entry of list.entries()) {
console.log(entry);
}
結果は次のようになります。
Array [ 0, <p> ] Array [ 1, #text "hey" ] Array [ 2, <span> ]