💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# ES6 Map 与 Set ## Map 对象 Map 对象保存键值对。任何值(对象或者原始值) 都可以作为一个键或一个值。 ``` <pre class="calibre13">``` ### Map 中的 key <span class="token">**</span>key 是字符串<span class="token">**</span> <span class="token2">var</span> myMap <span class="token">=</span> <span class="token2">new</span> <span class="token3">Map</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">var</span> keyString <span class="token">=</span> <span class="token4">"a string"</span><span class="token1">;</span> myMap<span class="token1">.</span><span class="token2">set</span><span class="token1">(</span>keyString<span class="token1">,</span> <span class="token4">"和键'a string'关联的值"</span><span class="token1">)</span><span class="token1">;</span> myMap<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span>keyString<span class="token1">)</span><span class="token1">;</span> <span class="token6">// "和键'a string'关联的值" </span> myMap<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span><span class="token4">"a string"</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// "和键'a string'关联的值" </span> <span class="token6">// 因为 keyString === 'a string'</span> ``` ``` ``` <pre class="calibre13">``` ### Map 对象的操作 <span class="token">**</span>Map 与 Array的转换<span class="token">**</span> <span class="token2">var</span> kvArray <span class="token">=</span> \<span class="token1">[</span>\<span class="token1">[</span><span class="token4">"key1"</span><span class="token1">,</span> <span class="token4">"value1"</span>\<span class="token1">]</span><span class="token1">,</span> \<span class="token1">[</span><span class="token4">"key2"</span><span class="token1">,</span> <span class="token4">"value2"</span>\<span class="token1">]</span>\<span class="token1">]</span><span class="token1">;</span> <span class="token6">// Map 构造函数可以将一个 二维 键值对数组转换成一个 Map 对象 </span> <span class="token2">var</span> myMap <span class="token">=</span> <span class="token2">new</span> <span class="token3">Map</span><span class="token1">(</span>kvArray<span class="token1">)</span><span class="token1">;</span> <span class="token6">// 使用 Array.from 函数可以将一个 Map 对象转换成一个二维键值对数组 </span> <span class="token2">var</span> outArray <span class="token">=</span> Array<span class="token1">.</span><span class="token2">from</span><span class="token1">(</span>myMap<span class="token1">)</span><span class="token1">;</span> ``` ``` ## Set 对象 Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。 ### Set 中的特殊值 Set 对象存储的值总是唯一的,所以需要判断两个值是否恒等。有几个特殊值需要特殊对待: - +0 与 -0 在存储判断唯一性的时候是恒等的,所以不重复; - undefined 与 undefined 是恒等的,所以不重复; - NaN 与 NaN 是不恒等的,但是在 Set 中只能存一个,不重复 ``` <pre class="calibre14">``` ### Set 对象作用 <span class="token">**</span>数组去重<span class="token">**</span> <span class="token2">var</span> mySet<span class="token">=</span> <span class="token2">new</span> <span class="token3">Set</span><span class="token1">(</span>\<span class="token1">[</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">,</span> <span class="token5">3</span><span class="token1">,</span> <span class="token5">4</span><span class="token1">,</span> <span class="token5">4</span>\<span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> \<span class="token1">[</span><span class="token">...</span>mySet\<span class="token1">]</span><span class="token1">;</span> <span class="token6">// \[1, 2, 3, 4\]</span> ``` ```