(1) Map
- key, value를 저장 (key에 어떤 데이터타입도 다 들어올 수 있다는 점이 객체와의 차이점 )
- Map 은 키가 정렬된 순서로 저장됨
- 기능: 검색 및 추가(Set) , 삭제(delete), 제거(clear) , 존재여부확인(size)
const myMap = new Map();
myMap.set("key","value")<-이렇게 Key와 value를 생성한다
//Map에서는 keys(), values(), entries() 메소드를 사용하여 키, 값 및 키-값 쌍을 반복할 수 있다.
const myMap= new Map();
myMap.set('one', 1);
myMap.set('two', 2);
myMap.set('three', 3);
console.log(myMap.keys());
// [Map Iterator] {"one", "two", "three"}
console.log(myMap.values())
// [Map Iterator] {1,2,3}
for(const key of myMap.keys()){
console.log(key);
}
//one / two / three
for (const value of myMap.values()){
console.log(value);
}
// 1 / 2 / 3
console.log(myMap.entreis());
// [Mat Iterator] {["one",1], ["two", 2], ["three", 3]}
for (const entry of myMap.entries()){
console.log(entry)
}
// ["one",1]
// ["two", 2]
// ["three", 3]
console.log(myMap.size); // 3 -map의 사이즈 (길이)
console.log(myMap.has("two")) // true - key기반 검색
(2) Set
- Set은 고유한 값을 저장하는 자료 구조.
- Set은 값만 저장하며, 키를 저장하지 않는다.
- Set은 값이 중복되지 않는 유일한 요소로만 구성.
- Set 기능 : 값 추가 및 검색, 값 삭제, 모든 값 제거, Set 크기 및 존재 여부 확인
const mySet = new Set();
mySet.add("value1");
mySet.add("value2");//<-이렇게 Key 없이 value만 넣어서 생성한다
mySet.add("value2");//"값이 중복되지 않는 유일한 요소로만 구성"=> 중복되는 값은 합쳐진다.
console.log(mySet.size); //2
console.log(mySet.has("value1")); //true
// Iterator
for (const value of mySet.values()){
console.log(value)
}
// value1
// value2