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); //falseMap的迭代:
let myMap = new Map();
myMap.set("23","喬丹");
myMap.set("33","皮蓬");
myMap.set("99","羅德曼");
//循環(huán)鍵
for (let key of myMap.keys()) {
console.log(key);
}
//循環(huán)值
for (let value of myMap.values()) {
console.log(value);
}
//循環(huán)鍵和值
for (let [key, value] of myMap) {
console.log(key + " = " + value);
}
//或
for (let [key, value] of myMap.entries()) {
console.log(key + " = " + value);
}
//使用forEach循環(huán)
myMap.forEach(function(value,key){
console.log(key + "=" + value);
},myMap);Map 與 Array的轉換:
//二維數(shù)組轉換成map對象
let arr = [[23,"喬丹"],[33,"皮蓬"],[99,"羅德曼"]];
let myMap = new Map(arr);
for (let [key, value] of myMap) {
console.log(key + " = " + value);
}
//map對象轉換成二維數(shù)組
let outArr = Array.from(myMap);
console.log(outArr);Map的克?。?/p>
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"); //這里體現(xiàn)了類型的多樣性
mySet.add(2);
mySet.add(1); //這里添加不了,這里體現(xiàn)了值的唯一性
console.log(mySet); //{1,"hello",2}
console.log(mySet.has(3)); //false, 是否含有3以下代碼體現(xiàn)了對象之間引用不同不恒等,即使值相同,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只出現(xiàn)一次)Set對象的作用:
//數(shù)組去重復 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 //數(shù)組求并集 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 //數(shù)組求交集 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
到此這篇關于ES6基礎語法之Map和Set對象的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
在JavaScript中處理時間之getHours()方法的使用
這篇文章主要介紹了在JavaScript中處理時間之getHours()方法的使用,是JS入門學些中的基礎知識,需要的朋友可以參考下2015-06-06
THREE.JS入門教程(4)創(chuàng)建粒子系統(tǒng)
Three.js是一個偉大的開源WebGL庫,WebGL允許JavaScript操作GPU,在瀏覽器端實現(xiàn)真正意義的3D本文將介紹創(chuàng)建一個粒子系統(tǒng)/風格/引入物理等等,感興趣的朋友可以了解下哦,希望本文對你有所幫助2013-01-01
javascript函數(shù)中的arguments參數(shù)
arguments當然只在function體內才有意義, arguments.length 返回的是傳入function的實參個數(shù)2010-08-08
不得不看之JavaScript構造函數(shù)及new運算符
這篇文章主要介紹了JavaScript構造函數(shù)及new運算符,通過認識new運算符,代碼解讀,重點解析,new存在的意義,總結等全面介紹了知識點,具體操作步驟大家可查看下文的詳細講解,感興趣的小伙伴們可以參考一下。2017-08-08

