JavaScript的Map和Set
1、map:映射(通過key獲得value)、增、刪
2、set:增、刪、判斷是否包含某個元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript學習</title>
<script>
'use strict';
let map = new Map([["東大", 620], ["北航", 670], ["清華", 700]]);
alert(map.get("北航")); //670
map.set("復旦",690); //往映射里加入新的鍵值
map.delete("清華"); //刪除一個鍵值
</script>
</head>
<body>
</body>
</html>
2. Set
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript學習</title>
<script>
'use strict';
let set = new Set([1,3,3,3,5,7]);
set.add(2);
set.delete(7);
console.log(set.has(3));
</script>
</head>
<body>
</body>
</html>
3. 遍歷數組、Map和Set
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript學習</title>
<script>
'use strict';
<!--遍歷數組-->
let arr = [2,4];
for (let x of arr)
{
console.log(x);
}
<!--遍歷Map-->
let map = new Map([["東大", 620], ["北航", 670]]);
for (let x of map)
{
console.log(x);
}
<!--遍歷Set-->
let set = new Set([1,3]);
for (let x of set)
{
console.log(x);
}
</script>
</head>
<body>
</body>
</html>