Set和Map數據結構

#Set 定義:ES6定義的一種新的數據結構。Set是一個構造函數,用於生成set實例。 特色: 相似於數組,成員惟一es6

const s = new Set();
[1,2,3,4,5,6,6,].forEach( x => s.add(x));
for(let item of s){
	console.log(item)
}
//1 2 3 4 5 6
複製代碼

Set實例的屬性和方法

1.屬性數組

  • Set,prototype.constructor:構造函數,默認就是Set函數
  • Set.prototyp.size:返回Set實例的成員總數

2.方法bash

  • add(values):添加某個值,返回Set結構自己 向Set加入值不會發生類型轉換,值比較相似於「===」,可是區別是認爲NaN與自身相等數據結構

    let set = new Set();
    let a  = NaN;
    let b  =NaN;
    set.add(a).add(b);//返回set自己則能夠鏈式調用
    set;//Set {NaN}
    複製代碼
  • delete(values):刪除某個值,返回布爾值表示是否刪除成功函數

  • has(values):是否爲Set的成員,返回布爾值ui

  • clear():清空全部成員,沒有返回值spa

3.遍歷操做prototype

  • keys():返回鍵名的遍歷器
  • values():返回值的遍歷器(默認的遍歷器生成函數Set.prototype[Symbol.iterator] === Set.prototype.values //true
  • entries():返回鍵值對的遍歷器
  • forEach():使用回調函數遍歷每一個成員
let set = new Set([1,2,3]);
set.forEach((value,index) => console.log(value*2)) //2 //4 //6
複製代碼

Set結構和數組的轉換

1.Set轉數組3d

const set = new Set([1,2,3]);
let arr = [];

//使用Array.from
arr = Array.from(set);
arr; //[1, 2, 3]

//for of
for(let item of set){
	arr.push(item)
}
arr; //[1, 2, 3]
複製代碼

2.數組轉Setcode

let arr = [1,2,3];
const set = new Set(arr);
set;//Set(3) {1, 2, 3}
複製代碼

Set的簡單應用

1.數組去重

let arr =[2,3,4,5,5,4];
let newArr = [...new Set(arr)]
newArr;//(4) [2, 3, 4, 5]
複製代碼

2.並集、交集、差集

let a = new Set([1,2,3]);
let b = new Set([2,3,4]);

//並集
let union = new Set([...a,...b]);
//Set(4) {1, 2, 3, 4}

//交集
let intersect = new Set([...a].filter(x => b.has(x)));
//Set(2) {2, 3}

//差集
let difference = new Set([..a].filter(x => !b.has(x)));
//Set(1) {1}
複製代碼

#Map 定義:ES6定義的一種新的數據結構。Map是一個構造函數,用於生成map實例。 特色:相似於對象,是鍵值對集合,鍵範圍不限於字符串(各類類型包括對象)。

const m = new Map();
const  o = {P:"Hello World"};
m.set(o,"content");
m.get(o); //"content"
複製代碼

注意任何具備Iterator(遍歷器)接口且每一個成員都有一個雙元素數組的數據結構均可以當作Map構造函數的參數。

原生具有Iterator結構的數據結構以下:(相關閱讀Iterator

  • Array
  • Map
  • Set
  • String
  • TypedArry
  • 函數裏的arguments對象
  • NodeLIst 對象
//使用set來建立map
const set = new Set([
	['foo',1],
	['bar',2]
])
const m1 = new Map(set);
m1.get('foo'); //1

//使用數組建立map
const m2 = new Map([['bar',3]])//這裏若是使用new Map(['bar',3])則會報錯,由於數組中第一個元素不爲雙元素(Iterator value bar is not an entry object)
m2; //Map(1) {"bar" => 3}

//使用map建立map
const m3 = new Map(m2);
m3.get('bar');//3
複製代碼

Map的鍵實際是和內存綁定的,只要內存不同,就是爲兩個鍵。 若是鍵是一個簡單類型的值,嚴格相等則是爲一個鍵(NaN除外,是爲一個鍵)

const map = new Map();
map.set(['a'],5);
map.get(['a']); //undefined,數組是引用類型,定義set的['a']和get的['a']不是同一個數組對象

//繼續
let a = ['b'];
let b = a;
map.set(a,6);
map.get(b);//6,a和b爲引用了同一個數組對象
複製代碼

##Map實例的屬性和操做方法 1.屬性

  • size:返回Map成員總數;
  • set(key,value):設置key所對應的鍵值,返回整個Map結構。若是key已有則更新,沒有則建立。
  • get(key):讀取key對應的鍵值,找不到key則返回undefined。
  • has(key):某個鍵是否在Map結構中,返回布爾值。
  • delete(key):刪除某個鍵,返回布爾值。
  • clear():清除全部成員,沒有返回值

2.遍歷方法

  • keys():返回鍵名的遍歷器
  • values():返回值的遍歷器
  • entries():返回鍵值對的遍歷器(默認的遍歷器接口) map[Symbol.iterator] === map.entries //true
  • forEach():使用回調函數遍歷每一個成員 Map的遍歷順序就是插入順序
const map = new Map([
	['name','Lucas'],
	['age','18']
]);

for(let key of map.keys()){
	console.log(key)
}
//age name


for(let value of map.values()){
	console.log(value )
}
// Lucas 18

for(let item of map.entries()){
	console.log(item )
}
//(2) ["name", "Lucas"]
//(2) ["age", "18"]

複製代碼

##Map與其餘數據結構的轉換 1.Map與數組

//map轉數組
const myMap = new Map().set(true,7).set({foo:3},['abc']);
[...myMap]
//[[true,7],[{foo:3},['abc']]]

//數組轉map
new Map([
	[true,7],
	[{foo:3},['abc']]
])
//0:{true => 7}
  1:{Object => Array(1)}
複製代碼

2.Map與對象 若是Map的全部鍵都是字符串,則能夠轉爲對象。

//map轉對象
const myMap = new Map().set('name','zoom').set('age',8);
let obj = Object.create(null);
for(let [key,val] of myMap){
	obj[key] = val;
}
obj; //{name: "zoom", age: 8}

//對象轉map
const myMap2 = new Map();
let obj = {name: "zoom", age: 8};
for(let k of Object.keys(obj)){
	myMap2.set(k,obj[k]);
}
myMap2; //Map(2) {"name" => "zoom", "age" => 8}
複製代碼

3.Map與JSON 這一部分筆者沒有研究透徹,詳情可參考:Map

相關文章
相關標籤/搜索