-
[Javascript 이론] Map (feat. object)To infinity/Javascript 이론 2021. 7. 4. 21:30
1. 생성자함수
- const map = new Map( [[ 'key', 'value' ]] )
[ 'key', 'value' ]로만 적으면 오류남.
- Map 객체에는 중복된 키를 갖는 요소가 존재하지 않는다. (덮어쓰기가 되기 때문)
2. 요소 개수 확인
- const { size } = new Map([[ '1', 1 ] , [ '2', 2 ]]);
- getter 함수만 존재(setter X), 요소개수 변경 불가.
3. 요소 추가
- map.set('2', 2);
- 새로운 요소가 추가된 객체를 반환하므로 연속적으로 호출이 가능함.
- NaN과 Nan, 그리고 +0과 -0을 같다고 판단하기에 key로서의 중복추가가 불허하다.
- 키 타입에 제한없음
4. 요소 취득
-map.get
5. 요소 존재 여부 확인
- map.has
- 불리언 값을 반환
6. 요소삭제
- map.delete
- 삭제성공여부를 나타내는 불리언 값을 반환하므로 연속적으로 호출이 불가함.
ex) map.delete('lee).delete('kim') -> boolean.delete 가 되어버리니까.
7. 요소 일괄 삭제
- map.clear
- 언제나 undefined를 반환함
8. 요소순회
- map.forEach
Array.forEach와 유사하게 콜백함수와 forEach 메서드 콜백함수내부에서 this로 사용될 객체(옵션)를 인수로 전달.
첫번째 인수: 현재 순회 중인 요소값
두번째 인수: 현재 순회 중인 요소키
세번째 인수: 현재 순회 중인 Map 객체 자체
- map 객체는 이터러블임. 따라서 for..of문으로 순회할 수 있으며 스프레드 문법과 배열 디스트럭처링 할당의 대상이 될 수 있음.
- map 객체는 이터러블이면서 동시어 이터레이터인 객체를 반환하는 메서드를 제공함.
map.keys
map.values
map.entries
const lee = { name: 'lee' }; const kim = { name: 'kim' }; const map = new Map([[lee, 'developer'], [kim, 'designer']]); for (const key of map.keys()) { console.log(key); } for (const value of map.values()) { console.log(value); } for (const entry of map.entries()) { console.log(entry); }
'To infinity > Javascript 이론' 카테고리의 다른 글
[Javascript 이론] Object 프로퍼티 열거 시 특징 (for...in문) (0) 2021.07.08 [Javascript 이론] 객체의 프로퍼티 키, 값 불러오는 법 (0) 2021.07.08 [Javascript 이론] 일반함수, ES6 메서드 함수, 화살표함수 비교 (0) 2021.07.04 [Javascript] 객체리터럴 vs 생성자함수, 그리고 this (0) 2021.06.27 [Javascript] this (0) 2021.06.27