大网数据广告活动
Web 标准与前端开发

Headers: forEach() method

· 52CE 我爱测

The **`Headers.forEach()`** method executes a callback function once per each key/value pair in the `Headers` object.

Fetch API

The Headers.forEach() method executes a callback function once per each key/value pair in the Headers object.

Syntax

forEach(callbackFn)
forEach(callbackFn, thisArg)

Parameters

  • callbackFn
    • : Function to execute for each entry in the map. It takes the following arguments:
      • value
        • : Value of the currently visited header entry.
      • key
        • : Name of the currently visited header entry.
      • object
        • : The Headers object being iterated.
  • thisArg
    • : Value to use as this when executing callback.

Return value

undefined.

Description

The Headers.forEach() method executes the provided callback once for each key of the Headers which actually exist. It is not invoked for keys which have been deleted. However, it is executed for keys which are present but have the value undefined.

Examples

Printing the contents of Headers object

The following code logs a line for each key/value in the myHeaders object.

// Create a new test Headers object
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Cookie", "This is a demo cookie");
myHeaders.append("compression", "gzip");

// Display the key/value pairs
myHeaders.forEach((value, key) => {
  console.log(`${key} ==> ${value}`);
});

The result is:

compression ==> gzip
content-type ==> application/json
cookie ==> This is a demo cookie

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。