반응형
Map객체는 키값으로 구성되어 있으며
set(키, 값) 으로 집어 넣고
get(키)로 가져온다.
delete(키)로 삭제한다.
has(키)로 존재여부를 확인한다.
키를 정의해놓고 키를 알면 자유롭게 사용할 수 있어 매우 유용하다.
forEach, for ~ of 를 사용해서 전체 데이터를 가져올 수 도 있다.
// Create a Map
const fruits = new Map();
// Set Map Values
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);
console.log(fruits.get("apples");) // Returns 500
console.log(fruits.size);
fruits.delete("apples");
console.log(fruits.has("apples"));
// List all entries
let text = "";
fruits.forEach (function(value, key) {
text += key + ' = ' + value;
})
// List all entries
let text = "";
for (const x of fruits.entries()) {
text += x;
}
let map = new Map();
map.set("name", "John");
map.set("age", 25);
map.set("city", "New York");
for (let [key, value] of map) {
console.log(key + ": " + value);
}
반응형
'Javascript' 카테고리의 다른 글
switch 조건문 (0) | 2023.01.24 |
---|---|
for do while 반복문 (0) | 2023.01.24 |
날짜 객체 new Date() (0) | 2023.01.24 |
form selector 유효성점검 submit (0) | 2023.01.24 |
연산자 우선순위 (0) | 2023.01.24 |
댓글