ES6(Set,WeakSet,Map,WeakMap數(shù)據(jù)結(jié)構(gòu))

Set類型

數(shù)據(jù)特點:是一種集合,與數(shù)組類似,集合中的元素不能重復(fù)
// 聲明Set類型的數(shù)據(jù)
let list1 = new Set([1, 2, 3, 4, 5]);
console.log(list1);       // Set(5) {1, 2, 3, 4, 5}
console.log(list1.size);  // 5
使用場景:常用于去重
// 常用于去重
let list2 = new Set([1, 2, 2, 3, 4]);
console.log(list2);   // Set(4) {1, 2, 3, 4}

// 嚴(yán)格來說,2與'2'是不一致的,所以這里的結(jié)果與原先一致
let list3 = new Set([1, 2, '2', 3, 4]);
console.log(list3);   // Set(5) {1, 2, "2", 3, 4}
常用方法1:add(),has(),delete(),clear()
let list = new Set(['html', 'css']);

list.add('js');
list.add('js');                 // 這個操作是無效的
console.log(list);              // Set(3) {"html", "css", "js"}

console.log(list.has('html'));  // true

list.delete('html');
console.log(list);              // let list = new Set();

list.clear();
console.log(list);              // Set(0) {}
常用方法2:keys(),values(),entries()
let list = new Set(['html', 'css', 'js']);

for (let key of list.keys()) {
    console.log(key);
    // html
    // css
    // js
}

for (let value of list.values()) {
    console.log(value);
    // html
    // css
    // js
}

for (let [key, value] of list.entries()) {
    console.log(key, value);
    // html html
    // css css
    // js js
}

WeakSet類型

  • 集合中的元素只能是對象
  • 沒有clear(),size,不能遍歷
let weakList = new WeakSet();

weakList.add({});
// weakList.add(2);     // 報錯

console.log(weakList);  // WeakSet {{…}}

Map類型

數(shù)據(jù)特點:是一種集合,與對象類似,key可以是任意數(shù)據(jù)類型(比如數(shù)組,對象等)
聲明方式:
let map = new Map([
    ['a', 123],
    ['b', 456]
]);
console.log(map);           // Map(2) {"a" => 123, "b" => 456}
console.log(map.size);      // 2
常用方法1:get(),set(),has(),delete(),clear()
console.log(map.get('a'));  // 123

map.set('c',789);
console.log(map);           // Map(3) {"a" => 123, "b" => 456, "c" => 789}

console.log(map.has('a'));  // true

map.delete('a');
console.log(map);           // Map(1) {"b" => 456}

map.clear();
console.log(map);           // Map(0) {}
常用方法2:keys(),values(),entries()

與Set類似,省略...

WeakMap

WeakMap與Map的區(qū)別,和Set與WeakSet的區(qū)別是一樣,省略..

橫向?qū)Ρ龋篠et,Map,Array,Object

  • 優(yōu)先使用Map
  • 如果對數(shù)據(jù)唯一性要求較高,使用Set
  • 盡量不使用Array與Object
let map = new Map();
let set = new Set();
let array = [];
let obj = {};

let setItem = {t: 1};

// 增
map.set("t", 1);        // Map(1) {"t" => 1}
set.add(setItem);       // Set(1) [{t: 1}]
array.push({t: 1});     // [{t: 1}]
obj['t'] = 1;           // {t: 1}

// 查
map.has("t");                  // true
set.has(setItem);              // true
array.some(item => item.t);    // true
't' in obj;                    // true

// 改
map.set("t",2);                                     // Map(1) {"t" => 2}
item.t = 2;                                         // Set(1) [{t: 2}]
array.forEach(item => item.t ? item.t = 2 : null);  // Map(1) {"t" => 2}
obj['t'] = 2;                                       // {t: 1}

// 刪
map.delete("t");                                    // Map(0) {}
set.delete(setItem);                                // Set(0) {}
const index = array.findIndex(item => item.t);
array.splice(index,1);                              // []
delete obj["t"];                                    // {}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容