欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript中Set基本使用方法實例

 更新時間:2022年11月14日 09:19:55   作者:RayShyy  
因為Set中存放的數(shù)據(jù)都是不會重復(fù)的數(shù)據(jù),我們在編寫JS代碼的時候,因此我們可以利用Set來幫助我們更便捷地完成許多的事,下面這篇文章主要給大家介紹了關(guān)于JavaScript中Set基本使用方法的相關(guān)資料,需要的朋友可以參考下

介紹

ECMAScript 6 新增的 Set 是一種新集合類型,為這門語言帶來集合數(shù)據(jù)結(jié)構(gòu)。Set 在很多方面都像是加強的 Map,這是因為它們的大多數(shù) API 和行為都是共有的。

基本API

1. 創(chuàng)建Set實例

使用 new 關(guān)鍵字和 Set 構(gòu)造函數(shù)可以創(chuàng)建一個空集合:

const s = new Set();

如果想在創(chuàng)建的同時初始化實例,則可以給 Set 構(gòu)造函數(shù)傳入一個可迭代對象,其中需要包含插入到新集合實例中的元素(Set 可以包含任何 JavaScript 數(shù)據(jù)類型作為值):

const s = new Set(["val1", 1, true, {}, undefined, function fun() {}]);

注意:Set結(jié)構(gòu)不會添加重復(fù)的值

const s = new Set([1, 1, 2, 3, 4, 4, 5, 6, 7, 4, 2, 1]);
Array.from(s); //  [1, 2, 3, 4, 5, 6, 7]

經(jīng)常用Set解決數(shù)組去重問題

const arr = [1, 2, 3, 3, 4, 5, 4, 4, 2, 1, 3];
Array.from(new Set(arr)); // [1, 2, 3, 4, 5]

2. Set實例轉(zhuǎn)數(shù)組

const s = new Set([1, 2, 3]);
Array.from(s); // [1, 2, 3]

3. size屬性

size: 獲取Set實例的元素個數(shù):

const s = new Set([1, 2, 3]);
s.size; // 3

4. add()

add(): 添加元素:

const s = new Set();
s.add(1).add(2).add(3);
Array.from(s); // [1, 2, 3]

5. has()

has(): 查詢Set實例是否存在某元素(返回布爾值):

const s = new Set();
s.add(1).add(2).add(3);
s.has(1); // true

6. delete()

delete(): 刪除Set實例中某個元素(返回布爾值):

const s = new Set();
s.add(1).add(2);
s.delete(1);
Array.from(s); // [2]

7. clear()

clear(): 清空Set實例:

const s = new Set();
s.add(1).add(2).add(3);
Array.from(s); // [1, 2, 3]
s.clear();
Array.from(s); // []

8. 迭代

keys():返回鍵名;

values(): 返回鍵值;

entries(): 返回鍵值對;

鍵名=鍵值

const s = new Set();
s.add(1).add(2).add(3);
Array.from(s.keys()); // [1, 2, 3]
Array.from(s.values()); // [1, 2, 3]
Array.from(s.entries()); // [[1, 1], [2, 2], [3, 3]]

for-of:

const s = new Set();
s.add(1).add(2).add(3);
for (const i of s) {
	console.log(i);
}
// 1
// 2
// 3

forEach

const s = new Set();
s.add(1).add(2).add(3);
s.forEach((value, key) => console.log(key + ' : ' + value));
// 1 : 1
// 2 : 2
// 3 : 3

補充:JS中Set的操作方法

(1):數(shù)組與Set之間的轉(zhuǎn)換:

一:數(shù)組轉(zhuǎn)Set:

     var arr = ["1","2","1","2","3","1"];
     var set = new Set(arr);
     //得到一個新的Set:{"1","2","3"};

二:Set轉(zhuǎn)數(shù)組:

     var arr1= Array.from(set );
     //得到一個新的數(shù)組:["1","2","3"];

(2):使用Set給數(shù)組去重:

            //定義一個新的數(shù)組:
            var arr = ["1","2","1","2","3","1"];

方法一:

         var arr1 = Array.from(new Set(arr));
         //得到一個新的數(shù)組:["1","2","3"];

方法二:

         var arr1 = [...new Set(arr)];
         //得到一個新的數(shù)組:["1","2","3"];

(3):求兩個Set的并集,交集,差集:

            var arr1 = ["1","2","3"];
            var arr2 = ["1","2"];
            var set1= new Set(arr1);
            var set2= new Set(arr2);
            
            //并集后:
            var newSet1 = new Set([...set1,...set2]);
            //得到一個新的Set:{"1","2","3"};
            
            //交集后:
            var newSet2 = new Set([...set1].filter(x => set2.has(x)));
            //得到一個新的Set:{"1", "2"};
            
            //差集后:
            var newSet3 = new Set([...set1].filter(x => !set2.has(x)));
            //得到一個新的Set:{"3"};

至此,在JS中使用Set的使用方法暫時寫到這兒,以后想起來再更新。

總結(jié)

到此這篇關(guān)于JavaScript中Set基本使用方法的文章就介紹到這了,更多相關(guān)js中Set使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論