-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.html
46 lines (40 loc) · 1.09 KB
/
map.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Document</title>
</head>
<body>
<script>
let m = new Map();
m.set('dog', 'ワンワン');
m.set('cat', 'ニャー');
m.set('mouse', 'チュー');
console.log(m);
console.log(m.size); // 結果:3
console.log(m.get('dog')); // 結果:ワンワン
console.log(m.has('cat')); // 結果:true
// キーを順番に取得
for (let key of m.keys()) {
console.log(key); // 結果:dog、cat、mouse
}
// 値を順番に取得
for (let value of m.values()) {
console.log(value); // ワンワン、ニャー、チュー
}
//キー/値を順に取得
for (let [key, value] of m.entries()) {
console.log(`${key}: ${value}`);
}
// キー dogを削除
m.delete('dog');
console.log(m.size); // 結果:2
m.clear();
console.log(m.size); // 結果:0
</script>
</body>
</html>