forEach()
方法按照插入顺序依次对 Map
中每个键/值对执行一次给定的函数
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
语法
myMap.forEach(callback([value][,key][,map])[, thisArg])
参数
callback
-
myMap
中每个元素所要执行的函数。它具有如下的参数value
可选- 每个迭代的值。
key
可选- 每个迭代的键。
map
可选- 被迭代的map(上文语法框中的
myMap
)。
thisArg
可选- 在
callback
执行中使用的this
的值。
返回值
描述
forEach
方法会对map中每个真实存在的键执行一次给定的 callback
函数。它不会对被删除的键执行函数。然而,它会对每个值为 undefined
的键执行函数。
callback
接收三个参数:
- 当前的
value
- 当前的
key
- 正在被遍历的
Map
对象
如果 forEach
中含有 thisArg
参数,那么每次 callback
被调用时,都会被用作 this
的值。否则,undefined
将会被用作 this
的值。按照函数观察到 this
的常用规则,callback
函数最终可观察到 this
值。
每个值只被访问一次,除非它被删除了或者在 forEach
结束前被改变了。callback
不会对在被访问前就删除的元素执行。在 forEach
结束前被添加的元素将会被访问。
forEach
会对 Map
对象中的每个元素执行一次 callback
。它不会返回值。
示例
输出一个 Map 对象中的内容
以下的代码在每行中打印一个 Map
对象中的元素
function logMapElements(value, key, map) {
console.log(`map.get('${key}') = ${value}`)
}
new Map([['foo', 3], ['bar', {}], ['baz', undefined]]).forEach(logMapElements)
// logs:
// "map.get('foo') = 3"
// "map.get('bar') = [object Object]"
// "map.get('baz') = undefined"
规范
浏览器兼容
BCD tables only load in the browser
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.