js中 set, map區別


 js中 set, map區別:https://blog.csdn.net/mhbsoft/article/details/85295843

 

Set 對象類似於數組,且成員的值都是唯一的。

const arr = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1];
const set = new Set();
arr.forEach(item => set.add(item));
console.log(set); // 1, 2, 3, 4, 5
// 數組快速去重
console.log([...new Set(arr)]); //[1, 2, 3, 4, 5]

const arr = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1];
const set = new Set();
arr.forEach(item => set.add(item));
console.log(set); // 1, 2, 3, 4, 5
// 數組快速去重
console.log([...new Set(arr)]); //[1, 2, 3, 4, 5]


Map 對象是鍵值對集合,和 JSON 對象類似,但是 key 不僅可以是字符串還可以是對象

var map = new Map();
var obj = { name: '小緣', age: 14 };
map.set(obj, '小緣喵');
map.get(obj); // 小緣喵
map.has(obj); // true
map.delete(obj) ;// true
map.has(obj); // false
————————————————
版權聲明:本文為CSDN博主「mhbsoft」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/mhbsoft/article/details/85295843


免責聲明!

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



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