{"id":472,"date":"2025-12-05T10:22:57","date_gmt":"2025-12-05T02:22:57","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=472"},"modified":"2025-12-05T10:22:57","modified_gmt":"2025-12-05T02:22:57","slug":"javascript-%e4%b8%ad-set-%e5%92%8c-map-%e7%9a%84%e7%a4%ba%e4%be%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript-%e4%b8%ad-set-%e5%92%8c-map-%e7%9a%84%e7%a4%ba%e4%be%8b%e8%af%a6%e8%a7%a3\/","title":{"rendered":"JavaScript\u00a0\u4e2d\u00a0Set\u00a0\u548c\u00a0Map\u00a0\u7684\u793a\u4f8b\u8be6\u89e3"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u8d85\u6e05\u6670\u3001\u53ef\u76f4\u63a5\u8fd0\u884c\u7684 JavaScript Set \u4e0e Map \u793a\u4f8b\u8be6\u89e3<\/strong>\uff0c\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528\uff0c\u5305\u542b\u5e38\u89c1\u7528\u6cd5\u3001\u5bb9\u6613\u5ffd\u7565\u7684\u7ec6\u8282\u3001\u4ee5\u53ca\u5de5\u7a0b\u7ea7\u573a\u666f\u793a\u4f8b\u3002\u9002\u5408\u5b66\u4e60\u3001\u9762\u8bd5\u3001\u9879\u76ee\u5f15\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 \u4e00\u3001Set \u7684\u793a\u4f8b\u8be6\u89e3\uff08\u552f\u4e00\u503c\u96c6\u5408\uff09<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1\ufe0f\u20e3 Set \u57fa\u672c\u7528\u6cd5<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst set = new Set();\n\nset.add(1);\nset.add(2);\nset.add(2);  \/\/ \u91cd\u590d\u503c\u4f1a\u88ab\u5ffd\u7565\n\nconsole.log(set);  \/\/ Set { 1, 2 }\nconsole.log(set.size); \/\/ 2\nconsole.log(set.has(1)); \/\/ true\nset.delete(1);\nconsole.log(set.has(1)); \/\/ false\n\n<\/pre><\/div>\n\n\n<p>\u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u52a8\u53bb\u91cd<\/li>\n\n\n\n<li>\u53ea\u80fd\u5b58\u653e\u552f\u4e00\u503c<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2\ufe0f\u20e3 \u7528 Set \u8fdb\u884c\u6570\u7ec4\u53bb\u91cd\uff08\u6700\u5e38\u89c1\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst arr = &#x5B;1, 2, 3, 2, 1];\nconst unique = &#x5B;...new Set(arr)];\n\nconsole.log(unique);  \/\/ &#x5B;1, 2, 3]\n\n<\/pre><\/div>\n\n\n<p>\u8fd9\u662f\u524d\u7aef\u6700\u5e38\u89c1\u7684 Set \u4f7f\u7528\u573a\u666f\u4e4b\u4e00\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">3\ufe0f\u20e3 Set \u5bf9 NaN \u7684\u5904\u7406\u793a\u4f8b<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst set = new Set(&#x5B;NaN, NaN]);\n\nconsole.log(set.size); \/\/ 1 \uff08NaN === NaN \u5728 Set \u4e2d\u4e3a true\uff09\n\n<\/pre><\/div>\n\n\n<p>\u5bf9\u6bd4\u6570\u7ec4\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;NaN].includes(NaN); \/\/ false\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">4\ufe0f\u20e3 Set \u53ef\u4ee5\u5b58\u50a8\u5bf9\u8c61\uff08\u6309\u5f15\u7528\u5224\u65ad\u662f\u5426\u76f8\u540c\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst set = new Set();\nconst obj = { a: 1 };\n\nset.add(obj);\nset.add({ a: 1 }); \/\/ \u5f15\u7528\u4e0d\u540c\uff0c\u88ab\u5f53\u4f5c\u65b0\u5143\u7d20\n\nconsole.log(set.size); \/\/ 2\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">5\ufe0f\u20e3 Set \u904d\u5386\u793a\u4f8b<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst set = new Set(&#x5B;&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]);\n\nfor (const v of set) {\n  console.log(v);\n}\n\nset.forEach(v =&gt; console.log(&quot;foreach:&quot;, v));\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 6\ufe0f\u20e3 \u4f7f\u7528 Set \u505a\u96c6\u5408\u8fd0\u7b97\uff08\u9ad8\u7ea7\u793a\u4f8b\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e76\u96c6<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst a = new Set(&#x5B;1, 2]);\nconst b = new Set(&#x5B;2, 3]);\n\nconst union = new Set(&#x5B;...a, ...b]);\nconsole.log(union); \/\/ Set {1,2,3}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u4ea4\u96c6<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst intersection = new Set(&#x5B;...a].filter(x =&gt; b.has(x)));\nconsole.log(intersection); \/\/ Set {2}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5dee\u96c6<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst difference = new Set(&#x5B;...a].filter(x =&gt; !b.has(x)));\nconsole.log(difference); \/\/ Set {1}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 \u4e8c\u3001Map \u7684\u793a\u4f8b\u8be6\u89e3\uff08\u952e\u503c\u5bf9\u54c8\u5e0c\u7ed3\u6784\uff09<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1\ufe0f\u20e3 \u57fa\u672c\u7528\u6cd5<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst map = new Map();\n\nmap.set(&quot;name&quot;, &quot;Jack&quot;);\nmap.set(&quot;age&quot;, 18);\n\nconsole.log(map.get(&quot;name&quot;)); \/\/ Jack\nconsole.log(map.has(&quot;age&quot;));  \/\/ true\n\nmap.delete(&quot;age&quot;);\n\nconsole.log(map.size); \/\/ 1\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2\ufe0f\u20e3 Map \u7684 key \u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\uff08\u91cd\u70b9\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst map = new Map();\n\nconst obj = { id: 1 };\nmap.set(obj, &quot;this is object&quot;);\n\nmap.set(() =&gt; {}, &quot;function key&quot;);\nmap.set(NaN, &quot;NaN key&quot;);\n\nconsole.log(map);\n\n<\/pre><\/div>\n\n\n<p>Object \u4e0d\u80fd\u505a\u5230\u8fd9\u4e00\u70b9\uff08\u53ea\u80fd\u7528\u5b57\u7b26\u4e32\u5f53 key\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">3\ufe0f\u20e3 Map \u904d\u5386\u793a\u4f8b<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst map = new Map(&#x5B;\n  &#x5B;&#039;a&#039;, 1],\n  &#x5B;&#039;b&#039;, 2]\n]);\n\nfor (const &#x5B;key, value] of map) {\n  console.log(key, value);\n}\n\nmap.forEach((value, key) =&gt; {\n  console.log(&quot;foreach:&quot;, key, value);\n});\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">4\ufe0f\u20e3 \u4f7f\u7528 Map \u6765\u7edf\u8ba1\u8bcd\u9891\uff08\u5de5\u7a0b\u4e2d\u6700\u5e38\u89c1\u573a\u666f\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst text = &quot;banana&quot;;\n\nconst freq = new Map();\n\nfor (const ch of text) {\n  freq.set(ch, (freq.get(ch) || 0) + 1);\n}\n\nconsole.log(&#x5B;...freq]);\n\/\/ &#x5B; &#x5B;&#039;b&#039;,1], &#x5B;&#039;a&#039;,3], &#x5B;&#039;n&#039;,2] ]\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">5\ufe0f\u20e3 \u5b66\u4f1a\u7528\u5bf9\u8c61\u5f53 key\uff08\u5f3a\u5927\u793a\u4f8b\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst user = { id: 1 };\nconst map = new Map();\n\nmap.set(user, { loginTime: Date.now() });\n\nconsole.log(map.get(user));\n\n\/\/ \u5220\u9664\u5bf9\u8c61\u4e0d\u4f1a\u8bef\u5220\u5176\u4ed6 key\nmap.delete(user);\n\n<\/pre><\/div>\n\n\n<p>Object \u5f53 key \u65f6\u6309\u5f15\u7528\u5224\u65ad\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 6\ufe0f\u20e3 \u5b9e\u73b0 LRU Cache\uff08Map \u7684\u7ecf\u5178\u5e94\u7528\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass LRU {\n  constructor(limit = 3) {\n    this.limit = limit;\n    this.cache = new Map();\n  }\n\n  get(key) {\n    if (!this.cache.has(key)) return null;\n\n    const value = this.cache.get(key);\n    this.cache.delete(key);         \/\/ \u79fb\u9664\u65e7\u4f4d\u7f6e\n    this.cache.set(key, value);     \/\/ \u63d2\u5230\u672b\u5c3e\uff08\u6700\u65b0\uff09\n    return value;\n  }\n\n  set(key, value) {\n    if (this.cache.has(key)) {\n      this.cache.delete(key);\n    }\n\n    this.cache.set(key, value);\n\n    if (this.cache.size &gt; this.limit) {\n      const firstKey = this.cache.keys().next().value;\n      this.cache.delete(firstKey);\n    }\n  }\n}\n\nconst lru = new LRU(2);\nlru.set(&quot;a&quot;, 1);\nlru.set(&quot;b&quot;, 2);\nlru.set(&quot;c&quot;, 3);  \/\/ a \u88ab\u6dd8\u6c70\nconsole.log(&#x5B;...lru.cache]); \/\/ &#x5B;&#x5B;&quot;b&quot;,2],&#x5B;&quot;c&quot;,3]]\n\n<\/pre><\/div>\n\n\n<p>Map \u7684 \u201c\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\u201d + \u201c\u5feb\u901f\u67e5\u8be2\u201d \u5b8c\u7f8e\u9002\u5408\u505a LRU\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 \u4e09\u3001Set \u4e0e Map \u5408\u4f5c\u793a\u4f8b\uff08\u9ad8\u7ea7\uff09<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1\ufe0f\u20e3 \u6839\u636e Map \u4e2d\u7684\u503c\u8fc7\u6ee4 Set<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst ids = new Set(&#x5B;1, 2, 3, 4]);\nconst info = new Map(&#x5B;&#x5B;1, &quot;ok&quot;], &#x5B;3, &quot;ok&quot;]]);\n\nconst filtered = new Set(&#x5B;...ids].filter(id =&gt; info.has(id)));\n\nconsole.log(filtered); \/\/ Set {1, 3}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2\ufe0f\u20e3 \u7528 Set \u53bb\u91cd Map \u7684 value<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst users = new Map(&#x5B;\n  &#x5B;&#039;a&#039;, &#039;Tom&#039;],\n  &#x5B;&#039;b&#039;, &#039;Jack&#039;],\n  &#x5B;&#039;c&#039;, &#039;Tom&#039;]\n]);\n\nconst uniqueNames = new Set(users.values());\n\nconsole.log(uniqueNames); \/\/ Set {&#039;Tom&#039;,&#039;Jack&#039;}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 \u56db\u3001Set \u4e0e Map \u7684\u5e38\u89c1\u9677\u9631\u793a\u4f8b<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u27571\ufe0f\u20e3 Set \u5bf9\u5bf9\u8c61\u4e0d\u53bb\u91cd\uff08\u6309\u5f15\u7528\u5224\u65ad\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnew Set(&#x5B;{a:1}, {a:1}]).size; \/\/ 2\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u27572\ufe0f\u20e3 Map \u4e2d key \u662f\u5bf9\u8c61\u65f6\u5fc5\u987b\u4fdd\u7559\u5f15\u7528<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst map = new Map();\nmap.set({a:1}, 123);\n\nmap.get({a:1}); \/\/ undefined\uff0c\u56e0\u4e3a\u4e0d\u662f\u540c\u4e00\u4e2a\u5f15\u7528\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u27573\ufe0f\u20e3 JSON \u65e0\u6cd5\u76f4\u63a5\u5e8f\u5217\u5316 Set\/Map<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nJSON.stringify(new Set(&#x5B;1,2])); \/\/ {}\n\n<\/pre><\/div>\n\n\n<p>\u6b63\u786e\u505a\u6cd5\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nJSON.stringify(&#x5B;...new Set(&#x5B;1,2])]); \/\/ &#x5B;1,2]\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u8d85\u6e05\u6670\u3001\u53ef\u76f4\u63a5\u8fd0\u884c\u7684 JavaScript Set \u4e0e Map \u793a&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript-%e4%b8%ad-set-%e5%92%8c-map-%e7%9a%84%e7%a4%ba%e4%be%8b%e8%af%a6%e8%a7%a3\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-472","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=472"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/472\/revisions"}],"predecessor-version":[{"id":473,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/472\/revisions\/473"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}