1. 程式人生 > 其它 >四、ES6之Map和Set

四、ES6之Map和Set

一、Map物件

Map 物件儲存鍵值對。任何值(物件或者原始值) 都可以作為一個鍵或一個值。

Map中的鍵值是有序的。

let myMap = new Map();
myMap.set("23","喬丹");
myMap.set("33","皮蓬");
let name = myMap.get("33");
console.log(name);  //皮蓬
let has = myMap.has("24"); //查詢是否含有此鍵
console.log(has); //false

Map的迭代:

let myMap = new Map();
myMap.set("23","喬丹");
myMap.set("33","皮蓬");
myMap.set("99","羅德曼");
//迴圈鍵
for (let key of myMap.keys()) {
  console.log(key);
}		
//迴圈值
for (let value of myMap.values()) {
  console.log(value);
}			
//迴圈鍵和值
for (let [key, value] of myMap) {
  console.log(key + " = " + value);
}
//或
for (let [key, value] of myMap.entries()) {
  console.log(key + " = " + value);
}
			
//使用forEach迴圈
myMap.forEach(function(value,key){
	console.log(key + "=" + value);
},myMap);

Map 與 Array的轉換:

//二維陣列轉換成map物件
let arr = [[23,"喬丹"],[33,"皮蓬"],[99,"羅德曼"]];
let myMap = new Map(arr);
for (let [key, value] of myMap) {
  console.log(key + " = " + value);
}
//map物件轉換成二維陣列
let outArr = Array.from(myMap);
console.log(outArr);

Map的克隆:

let myMap1 = new Map([[23,"喬丹"],[33,"皮蓬"],[99,"羅德曼"]]);
let myMap2 = new Map(myMap1);
for (let [key, value] of myMap2) {
   console.log(key + " = " + value);
}

Map的合併(合併兩個 Map 物件時,如果有重複的鍵值,則後面的會覆蓋前面的)

let myMap1 = new Map([[23,"喬丹"],[33,"皮蓬"],[99,"羅德曼"]]);
let myMap2 = new Map([[23,"詹姆斯"],[24,"科比"],[11,"姚明"]]);
let myMap = new Map([...myMap1,...myMap2]); //合併之後詹姆斯會替換喬丹
for (let [key, value] of myMap) {
	console.log(key + " = " + value);
}

二、Set物件

Set 物件允許你儲存任何型別的唯一值,無論是原始值或者是物件引用。

Set 物件儲存的值總是唯一的,所以需要判斷兩個值是否恆等。有幾個特殊值需要特殊對待:

(1) +0 與 -0 在儲存判斷唯一性的時候是恆等的,所以不重複;

(2) undefined 與 undefined 是恆等的,所以不重複;

(3) NaN 與 NaN 是不恆等的,但是在 Set 中只能存一個,不重複。

let mySet = new Set();
mySet.add(1);
mySet.add("hello");  //這裡體現了型別的多樣性
mySet.add(2);
mySet.add(1); //這裡新增不了,這裡體現了值的唯一性
console.log(mySet); //{1,"hello",2}
console.log(mySet.has(3)); //false, 是否含有3

以下程式碼體現了物件之間引用不同不恆等,即使值相同,Set 也能儲存

let mySet = new Set();
let o = {a: 1, b: 2}; 
mySet.add(o);
mySet.add({a: 1, b: 2});
console.log(mySet);

Set型別轉換:

//Array 轉 Set
let arr = ["喬丹","皮蓬","羅德曼"];
let mySet = new Set(arr);
console.log(mySet);

//Set轉Array(使用...)
let mySet = new Set();
mySet.add("喬丹");
mySet.add("皮蓬");
mySet.add("羅德曼");
let arr = [...mySet];
console.log(arr);

//字串轉Set(注:Set中toString方法是不能將Set轉換成String)
let mySet = new Set("hello");
console.log(mySet);  //h e l o (兩個l只出現一次)

Set物件的作用:

//陣列去重複
let mySet = new Set([1,2,1,2,3,3,4,5,6,4,7]);
let arr = [...mySet];
console.log(arr); //1,2,3,4,5,6,7

//陣列求並集
let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);
let union = new Set([...a, ...b]);
let arr = [...union];
console.log(arr); //1, 2, 3, 4

//陣列求交集
let a = new Set([1, 2, 3]);
let b = new Set([4, 3, 2]);
let intersect = new Set([...a].filter(p=>b.has(p)));
let arr = [...intersect];
console.log(arr); //2, 3

本文來自部落格園,作者:農碼一生,轉載請註明原文連結:https://www.cnblogs.com/wml-it/p/15967783.html


技術的發展日新月異,隨著時間推移,無法保證本部落格所有內容的正確性。如有誤導,請大家見諒,歡迎評論區指正!
個人開原始碼連結,歡迎點亮:
GitHub:https://github.com/ITMingliang
Gitee:https://gitee.com/mingliang_it
GitLab:https://gitlab.com/ITMingliang
進開發學習交流群: