Iterator.prototype.forEach()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

forEach()Iterator インスタンスのメソッドで、 Array.prototype.forEach() と似ています。指定された関数を、イテレーターによって生成されたそれぞれの要素に対して 1 回ずつ指定された関数を実行します。

構文

js
forEach(callbackFn)

引数

callbackFn

イテレーターによって生成された各要素に対して実行する関数。その返値は破棄されます。関数は次の引数で呼び出されます。

element

処理中の現在の要素です。

index

処理中の現在の要素のインデックスです。

返値

undefined です。

解説

forEach() はイテレーターを反復処理し、それぞれの要素に対して一度ずつ callbackFn 関数を呼び出します。 他のほとんどのイテレーターヘルパーメソッドとは異なり、これは遅延的ではないため、無限イテレーターでは動作しません。

forEach() の使用

js
new Set([1, 2, 3]).values().forEach((v) => console.log(v));

// ログ出力:
// 1
// 2
// 3

これは次のものと同等です。

js
for (const v of new Set([1, 2, 3]).values()) {
  console.log(v);
}

仕様書

Specification
Iterator Helpers
# sec-iteratorprototype.foreach

ブラウザーの互換性

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
forEach

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.
Has more compatibility info.

関連情報