js Map對象的用法 lambda 交集 並集 差集


js Map對象的用法

第一篇:

Map:

Map是一組鍵值對的結構,具有極快的查找速度。

舉個例子,假設要根據同學的名字查找對應的成績,如果用Array實現,需要兩個Array

var names = ['Michael', 'Bob', 'Tracy'];
var scores = [95, 75, 85];

給定一個名字,要查找對應的成績,就先要在names中找到對應的位置,再從scores取出對應的成績,Array越長,耗時越長。

如果用Map實現,只需要一個“名字”-“成績”的對照表,直接根據名字查找成績,無論這個表有多大,查找速度都不會變慢。用JavaScript寫一個Map如下:

var m = new Map([['Michael', 95], ['Bob', 75], ['Tracy', 85]]);

m.get('Michael'); // 95

初始化Map需要一個二維數組,或者直接初始化一個空MapMap具有以下方法:

復制代碼
var m = new Map(); // 空Map

m.set('Adam', 67); // 添加新的key-value

m.set('Bob', 59);

m.has('Adam'); // 是否存在key 'Adam': true

m.get(
'Adam'); // 67

m.
delete('Adam'); // 刪除key 'Adam'

m.get(
'Adam'); // undefined

復制代碼

由於一個key只能對應一個value,所以,多次對一個key放入value,后面的值會把前面的值沖掉:

復制代碼
var m = new Map();

m.set('Adam', 67);

m.set('Adam', 88);

m.get('Adam'); // 88

復制代碼

Set:

SetMap類似,也是一組key的集合,但不存儲value。由於key不能重復,所以,在Set中,沒有重復的key。

要創建一個Set,需要提供一個Array作為輸入,或者直接創建一個空Set

var s1 = new Set(); // 空Set

var s2 = new Set([1, 2, 3]); // 含1, 2, 3

重復元素在Set中自動被過濾:

var s = new Set([1, 2, 3, 3, '3']);

s; // Set {1, 2, 3, "3"}

注意數字3和字符串'3'是不同的元素。

通過add(key)方法可以添加元素到Set中,可以重復添加,但不會有效果:

復制代碼
s.add(4);

s; // Set {1, 2, 3, 4}

s.add(
4);

s; // 仍然是 Set {1, 2, 3, 4}

復制代碼

通過delete(key)方法可以刪除元素:

復制代碼
var s = new Set([1, 2, 3]);

s; // Set {1, 2, 3}

s.
delete(3);

s; // Set {1, 2}

復制代碼

小結

MapSet是ES6標准新增的數據類型,請根據瀏覽器的支持情況決定是否要使用

Set和Map主要的應用場景在於數組去重數據存儲

Set是一種叫做集合的數據結構,Map是一種叫做字典的數據結構

集合

  • 集合是由一組無序且唯一(即不能重復)的項組成的,可以想象成集合是一個既沒有重復元素,也沒有順序概念的數組
  • ES6提供了新的數據結構Set。它類似於數組,但是成員的值都是唯一的,沒有重復的值
  • Set 本身是一個構造函數,用來生成 Set 數據結構
  • 這里說的Set其實就是我們所要講到的集合,先來看下基礎用法
復制代碼
const s = new Set();
[
2, 3, 5, 4, 5, 2, 2].forEach(x => s.add(x));
for (let i of s) { console.log(i); // 2 3 5 4 } // 去除數組的重復成員 let array = [1,2,1,4,5,3];

[...new Set(array)] // [1, 2, 4, 5, 3]

復制代碼

Set實例的屬性和方法

  • Set的屬性:
    • size:返回集合所包含元素的數量
  • Set的方法:
    • 操作方法
      • add(value):向集合添加一個新的項
      • delete(value):從集合中移除一個值
      • has(value):如果值在集合中存在,返回true,否則false
      • clear(): 移除集合里所有的項
    • 遍歷方法
      • keys():返回一個包含集合中所有鍵的數組
      • values():返回一個包含集合中所有值的數組
      • entries:返回一個包含集合中所有鍵值對的數組(感覺沒什么用就不實現了)
      • forEach():用於對集合成員執行某種操作,沒有返回值

創建一個集合

復制代碼
function Set(arr = []) {    // 可以傳入數組

    let items = {};
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0;  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 記錄集合中成員的數量</span>
} module.exports = Set;

復制代碼

復制代碼

這里用{}對象來表示集合,也是因為對象不允許一個鍵指向兩個不同的屬性,保證了集合里的元素都是唯一的

接下來,就需要按照ES6中Set類的實現,添加一些集合的操作方法

has方法

首先要實現的是has方法,因為在add和delete等其他方法中都會被調用,下面來看一下它的實現

復制代碼
function Set() {
let items </span>=<span style="color: rgba(0, 0, 0, 1)"> {};

</span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> has(val)方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.has = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(val) {

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 對象都有hasOwnProperty方法,判斷是否擁有特定屬性</span>

    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> items.hasOwnProperty(val);  

};

}

復制代碼

add方法

接下來要實現add方法

復制代碼
    // add(val)方法
<span style="color: rgba(0, 0, 255, 1)">this</span>.add = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(val) {

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        items[val] </span>=<span style="color: rgba(0, 0, 0, 1)"> val;

        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.size++;    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 累加集合成員數量</span>

        <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
};

復制代碼

對於給定的val,可以檢測是否存在於集合中

  • 如果不存在,就添加到集合中,返回true
  • 如果存在,就直接返回false,不做任何操作

delete和clear方法

繼續寫着,這回把兩個都寫上

復制代碼
    // delete(val)方法
<span style="color: rgba(0, 0, 255, 1)">this</span>.<span style="color: rgba(0, 0, 255, 1)">delete</span> = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(val) {

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        </span><span style="color: rgba(0, 0, 255, 1)">delete</span> items[val];  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 將items對象上的屬性刪掉</span>

        <span style="color: rgba(0, 0, 255, 1)">this</span>.size--<span style="color: rgba(0, 0, 0, 1)">;

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> clear方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.clear = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {

    items </span>= {};     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 直接將集合賦一個空對象即可</span>

    <span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

};</span></pre>
復制代碼

在delete方法中,判斷val是否存在於集合中,如果存在就直接從集合中刪掉,返回true

以上完成的都是操作方法,下面我們再來實現一下遍歷方法

keys、values方法

這兩個方法我們可以放在一起來實現,因為通過ES6對Object的擴展可以輕松實現對應的方法,下面看一下具體實現,上代碼:

復制代碼
    // keys()方法
<span style="color: rgba(0, 0, 255, 1)">this</span>.keys = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> Object.keys(items);  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回遍歷集合的所有鍵名的數組</span>
};
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> values()方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.values = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> Object.values(items);  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回遍歷集合的所有鍵值的數組</span>
};
復制代碼

使用一下看看

復制代碼
// set.js

const Set = require('./Set.js');    // 導入寫好的Set類

let set = new Set();

set.add(1);

set.add(3);

set.add(2);

console.log(set.keys()); // [ '1', '2', '3' ]

console.log(set.values());
// [ 1, 2, 3 ]

復制代碼

復制代碼

這里我們看到和ES6中的Set有點區別,因為Object的這幾個方法都是按照數值大小,從小到大遍歷的數組,所以大家知道這一點比較好,具體實現還是有些不同的,哈哈

forEach方法

ES6中Set結構的實例上帶的forEach方法,其實和數組的forEach方法很相似,只不過Set結構的鍵名就是鍵值,所以第一個參數與第二個參數的值永遠都是一樣的

下面就按照實現數組的forEach方法,我們來完成Set的forEach方法

復制代碼
// forEach(fn, context)方法
<span style="color: rgba(0, 0, 255, 1)">this</span>.forEach = <span style="color: rgba(0, 0, 255, 1)">function</span>(fn, context = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">) {

    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; <span style="color: rgba(0, 0, 255, 1)">this</span>.size; i++<span style="color: rgba(0, 0, 0, 1)">) {

        let item </span>=<span style="color: rgba(0, 0, 0, 1)"> Object.keys(items)[i];

        fn.call(context, item, item, items);     

    }

};</span></pre>
復制代碼

使用forEach方法

復制代碼
// set.js

const Set = require('./Set.js');

let set = new Set();

set.add(1);

set.add(4);

set.add('3');

set.forEach((value, key) => console.log(key + ' : ' + value)); // 1:1, 3:3, 4:4

let arr
= set.values(); // [ 1, 3, 4 ]

arr
= new Set(arr.map(x => x * 2)).values();

console.log(arr); // [ 2, 6, 8 ]

復制代碼

基本上實現了Set結構的方法,不過,發現一個問題,那就是每次添加一個元素都要add這樣寫起來確實好麻煩,Set是可以接收一個數組作為參數的,那么我們把這個也實現一下

復制代碼
function Set(arr = []) {    // 傳入接受的數組,如果沒有傳指定一個空數組做為初始值

    let items = {};
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> has方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.has = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> items.hasOwnProperty(val);

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> add方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.add = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果沒有存在items里面就可以直接寫入</span>

    <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        items[val] </span>=<span style="color: rgba(0, 0, 0, 1)"> val;

        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.size++<span style="color: rgba(0, 0, 0, 1)">;

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

};

arr.forEach((val, i) </span>=&gt; {   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍歷傳入的數組</span>

    <span style="color: rgba(0, 0, 255, 1)">this</span>.add(val);          <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 將數組里每一項值添加到集合中</span>
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 省略...</span>
}
復制代碼
復制代碼
function Set(arr = []) {
let items </span>=<span style="color: rgba(0, 0, 0, 1)"> {};

</span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> has方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.has = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> items.hasOwnProperty(val);

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> add方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.add = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果沒有存在items里面就可以直接寫入</span>

    <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        items[val] </span>=<span style="color: rgba(0, 0, 0, 1)"> val;

        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.size++<span style="color: rgba(0, 0, 0, 1)">;

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

};

arr.forEach((val, i) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {

    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.add(val);

});

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> delete方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.<span style="color: rgba(0, 0, 255, 1)">delete</span> = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        </span><span style="color: rgba(0, 0, 255, 1)">delete</span> items[val];  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 將items對象上的屬性刪掉</span>

        <span style="color: rgba(0, 0, 255, 1)">this</span>.size--<span style="color: rgba(0, 0, 0, 1)">;

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> clear方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.clear = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {

    items </span>=<span style="color: rgba(0, 0, 0, 1)"> {};

    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> keys方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.keys = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Object.keys(items);

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> values方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.values = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Object.values(items);

}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> forEach方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.forEach = <span style="color: rgba(0, 0, 255, 1)">function</span> (fn, context = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">) {

    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; <span style="color: rgba(0, 0, 255, 1)">this</span>.size; i++<span style="color: rgba(0, 0, 0, 1)">) {

        let item </span>=<span style="color: rgba(0, 0, 0, 1)"> Object.keys(items)[i];

        fn.call(context, item, item, items);

    }

}



</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 並集</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.union = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (other) {

    let union </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Set();

    let values </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.values();



    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

        union.add(values[i]);

    }

    values </span>= other.values();    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 將values重新賦值為新的集合</span>

    <span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

        union.add(values[i]);

    }



    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> union;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 交集</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.intersect = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (other) {

    let intersect </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Set();

    let values </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.values();

    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

        </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (other.has(values[i])) {

            intersect.add(values[i]);

        }

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> intersect;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 差集</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.difference = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (other) {

    let difference </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Set();

    let values </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.values();

    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">other.has(values[i])) {

            difference.add(values[i]);

        }

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> difference;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 子集</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.subset = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(other) {

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.size &gt;<span style="color: rgba(0, 0, 0, 1)"> other.size) {

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

    } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {

        let values </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.values();

        </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

            console.log(values[i])

            console.log(other.values())

            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">other.has(values[i])) {

                </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

            }

        }

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

};

}

module.exports = Set;

復制代碼
復制代碼
const Set = require('./Set.js');

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

console.log(set.keys()); // [ '1', '2', '3' ]

console.log(set.values());
// [ 1, 2, 3 ]

console.log(set.size);
// 3

set.
delete(1);

console.log(set.values()); // [ 2, 3 ]

set.clear();

console.log(set.size); // 0

// 並集

let a
= [1, 2, 3];

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

let union = new Set(a).union(b).values();

console.log(union); // [ 1, 2, 3, 4 ]

// 交集

let c
= new Set([4, 3, 2]);

let intersect = new Set([1,2,3]).intersect(c).values();

console.log(intersect); // [ 2, 3 ]

// 差集

let d
= new Set([4, 3, 2]);

let difference = new Set([1,2,3]).difference(d).values();

// [1,2,3]和[4,3,2]的差集是1

console.log(difference);
// [ 1 ]

復制代碼

ES6為Array增加了from函數用來將其他對象轉換成數組。

當然,其他對象也是有要求,也不是所有的,可以將兩種對象轉換成數組。

1.部署了Iterator接口的對象,比如:Set,Map,Array。

2.類數組對象,什么叫類數組對象,就是一個對象必須有length屬性,沒有length,轉出來的就是空數組。

轉換map

將Map對象的鍵值對轉換成一個一維數組。

實際上轉換出來的數組元素的序列是key1,value1,key2,value2,key3,value3.....

const map1 = new Map();
map1.set('k1', 1);
map1.set('k2', 2);
map1.set('k3', 3);
console.log('%s', Array.from(map1))

結果:

k1,1,k2,2,k3,3

轉換set

將Set對象的元素轉換成一個數組。

const set1 = new Set();
set1.add(1).add(2).add(3)
console.log('%s', Array.from(set1))

結果

1,2,3

轉換字符串

可以吧ascii的字符串拆解成一個數據,也可以准確的將unicode字符串拆解成數組。

console.log('%s', Array.from('hello world'))
console.log('%s', Array.from('\u767d\u8272\u7684\u6d77'))

結果:

h,e,l,l,o, ,w,o,r,l,d
白,色,的,海

類數組對象

一個類數組對象必須要有length,他們的元素屬性名必須是數值或者可以轉換成數值的字符。

注意:屬性名代表了數組的索引號,如果沒有這個索引號,轉出來的數組中對應的元素就為空。

console.log('%s', Array.from({
  0: '0',
  1: '1',
  3: '3',
  length:4
}))

結果:

0,1,,3

如果對象不帶length屬性,那么轉出來就是空數組。

console.log('%s', Array.from({
  0: 0,
  1: 1
}))

結果就是空數組。

對象的屬性名不能轉換成索引號時。

console.log('%s', Array.from({
  a: '1',
  b: '2',
  length:2
}))

結果也是空數組

Array.from可以接受三個參數

我們看定義:

Array.from(arrayLike[, mapFn[, thisArg]])

arrayLike:被轉換的的對象。

mapFn:map函數。

thisArg:map函數中this指向的對象。

第二個參數,map函數

用來對轉換中,每一個元素進行加工,並將加工后的結果作為結果數組的元素值。

console.log('%s', Array.from([1, 2, 3, 4, 5], (n) => n + 1))

結果:

上面的map函數實際上是給數組中的每個數值加了1。

2,3,4,5,6

第三個參數,map函數中this指向的對象

該參數是非常有用的,我們可以將被處理的數據和處理對象分離,將各種不同的處理數據的方法封裝到不同的的對象中去,處理方法采用相同的名字。

在調用Array.from對數據對象進行轉換時,可以將不同的處理對象按實際情況進行注入,以得到不同的結果,適合解耦。

這種做法是模板設計模式的應用,有點類似於依賴注入。

復制代碼

let diObj = {
  handle: function(n){
    return n + 2
  }
}

console.log('%s', Array.from(
[1, 2, 3, 4, 5],
function (x){
return this.handle(x)
},
diObj))

復制代碼

結果:

3,4,5,6,7
https://www.cnblogs.com/yuer20180726/p/11387699.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM