You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!
forEach() 메소드는 Map 오브젝트 내의 key/value 쌍의 개수 만큼 주어진 함수를 순서대로 실행합니다.
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[, thisArg])
파라미터
callback- 각각의 요소를 처리하기 위한 함수.
thisArg-
callback을 실행할때this로 사용되는 값.
Return value
설명
forEach 메서드는 map의 각각의 키마다 주어진 callback 함수를 실행합니다. 삭제된 키에대해서는 호출되지 않습니다. 그러나, 값이 존재하지만 undefined 인 값에 대해서는 실행됩니다.
callback 은 3가지 인수로 호출됩니다.
- 요소의 value
- 요소의 key
사용될 Map객체
thisArg 파라미터가 forEach 에 제공되면, this 값으로 사용하기 위해 호출될때 callback 으로 넘겨집니다. 그렇지 않으면 undefined 값이 this 값으로 넘겨질 것입니다. 궁극적으로 callback 으로 보여지게 된 this 값은 함수에 의해 보여지는 this 를 결정하기 위한 일반적인 규칙에 따라 결정됩니다.
각각의 value는 한번씩 사용됩니다. 다만 forEach 가 끝나기 전에 value가 삭제되거나 재추가 된 경우는 예외입니다. callback 은 사용되기 전에 삭제된 value에 의해 호출되지 않습니다. forEach 가 끝나기 전에 새롭게 추가된 value가 사용 됩니다.
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"
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Map.prototype.forEach' in that specification. |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) The definition of 'Map.prototype.forEach' in that specification. |
Draft |
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
forEach | Chrome Full support 38 | Edge Full support 12 | Firefox Full support 25 | IE Full support 11 | Opera Full support 25 | Safari Full support 8 | WebView Android Full support 38 | Chrome Android Full support 38 | Firefox Android Full support 25 | Opera Android Full support 25 | Safari iOS Full support 8 | Samsung Internet Android Full support 3.0 | nodejs Full support 0.12 |
Legend
- Full support
- Full support