Headers: forEach() メソッド
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
構文
js
forEach(callbackFn)
forEach(callbackFn, thisArg)
引数
callbackFn
-
このマップの各項目に対して実行される関数です。以下の引数を取ります。
thisArg
省略可-
callback
を実行するときにthis
として使用する値。
返値
undefined
です。
解説
Headers.forEach()
メソッドは、指定されたコールバックを、実際に存在するヘッダーの各キーに対して一度ずつ実行します。削除されたキーに対しては呼び出されません。しかし、存在しているが値が undefined であるキーに対しては実行されます。
例
Headers オブジェクトの内容を表示
以下のコードは myHeaders
オブジェクトの各キー/値に対して 1 行ずつログ出力します。
js
// 新しいテスト用 Headers オブジェクトを作成
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Cookie", "This is a demo cookie");
myHeaders.append("compression", "gzip");
// キーと値の組を表示
myHeaders.forEach((value, key) => {
console.log(`${key} ==> ${value}`);
});
結果は次の通りです。
compression ==> gzip content-type ==> application/json cookie ==> This is a demo cookie
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
forEach |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.