This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Map.prototype.get()
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월.
get() 메서드는Map 객체에서 특정 요소를 반환합니다. 만약 주어진 키와 관련된 값이 객체라면 해당 객체에 대한참조만 가져오고, 해당 객체에 대한 모든 변경은Map 내에서 효율적으로 수정됩니다.
In this article
시도해 보기
const map1 = new Map();map1.set("bar", "foo");console.log(map1.get("bar"));// Expected output: "foo"console.log(map1.get("baz"));// Expected output: undefined구문
js
get(key)매개변수
keyMap객체에서 반환받을 요소의 키
반환 값
명시된 키와 연관된 요소 혹은Map 객체에서 해당 키를 찾을 수 없는 경우undefined.
예제
>get() 사용하기
js
const myMap = new Map();myMap.set("bar", "foo");console.log(myMap.get("bar")); // "foo" 를 반환합니다.console.log(myMap.get("baz")); // undefined 을 반환합니다.get()을 사용하여 객체에 대한 참조 검색
js
const arr = [];const myMap = new Map();myMap.set("bar", arr);myMap.get("bar").push("foo");console.log(arr); // ["foo"]console.log(myMap.get("bar")); // ["foo"]맵이 원본 객체에 대한 참조만 보유하고 있다는 것은 해당 객체가 가비지 콜렉션되지 않을 수 있으며 이로 인해 예측하지 못한메모리 문제가 일어날 수 있음을 의미합니다. 만약 맵에 저장되어 있는 객체가 원본 객체와 동일한 수명을 가지게 하려면WeakMap을 고려하시기 바랍니다.
명세서
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-map.prototype.get> |