This page was translated from English by the community. Learn more and join the MDN Web Docs community.
Map.prototype.forEach()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
Map 인스턴스의 forEach() 메서드는 이 Map 객체의 키/값 쌍마다 각각 제공된 함수를 삽입되었던 순서대로 실행합니다.
시도해 보기
function logMapElements(value, key, map) {
console.log(`m[${key}] = ${value}`);
}
new Map([
["foo", 3],
["bar", {}],
["baz", undefined],
]).forEach(logMapElements);
// Expected output: "m[foo] = 3"
// Expected output: "m[bar] = [object Object]"
// Expected output: "m[baz] = undefined"
구문
forEach(callbackFn)
forEach(callbackFn, thisArg)
매개변수
callbackFn-
맵의 각 항목에 대해 실행할 함수입니다. 이 함수는 다음 인수를 사용하여 호출됩니다.
thisArgOptional-
callbackFn을 실행하고 있을 때this로 사용하는 값.
반환 값
없음 (undefined).
설명
forEach 메서드는 실제로 존재하는 맵의 각 키에 대해 제공된 callback을 한 번씩 실행합니다. 삭제된 키에 대해서는
호출되지 않습니다. 그러나 값은 존재하지만 undefined값일 경우에는 callback이 실행됩니다.
callback은 인자 3개와 함께 호출됩니다.
- 항목의
value - 항목의
key - 순회하고 있는
Map객체
thisArg가 forEach의 매개 변수로 제공되면 호출 시 this 값으로 사용하기 위해 callback에 전달됩니다.
그렇지 않으면 undefined 값이 this 값으로 사용되도록 전달됩니다. callback이 궁극적으로 관찰할 수 있는
this 값은 함수가 보는 this를 결정하기 위한 일반적인 규칙에 따라 결정됩니다.
forEach가 완료되기 전에 삭제했다가 다시 추가한 경우를 제외하고 각 값은 한 번씩 방문합니다. 방문하기 전에 삭제된 값에 대해서는
callback이 호출되지 않습니다. forEach가 완료되기 전에 추가된 새 값은 나중에 방문합니다.
예제
>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"
명세서
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-map.prototype.foreach> |
