{"id":3449,"date":"2024-11-10T12:42:17","date_gmt":"2024-11-10T04:42:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3449\/"},"modified":"2024-11-10T12:42:17","modified_gmt":"2024-11-10T04:42:17","slug":"js%e5%a6%82%e4%bd%95%e5%ad%98map","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3449\/","title":{"rendered":"js\u5982\u4f55\u5b58map"},"content":{"rendered":"<blockquote><p>\n  javascript \u63d0\u4f9b\u4e86 map \u5bf9\u8c61\u6765\u5b58\u50a8\u952e\u503c\u5bf9\uff0c\u5b83\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u67e5\u627e\u548c\u904d\u5386\u529f\u80fd\uff0c\u4e0e\u4f20\u7edf\u5bf9\u8c61\u76f8\u6bd4\u66f4\u9002\u5408\u5904\u7406\u590d\u6742\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110812341069565.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5b58map\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5b58map\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u5b58\u50a8 Map<\/strong><\/p>\n<p>JavaScript \u4e2d\u7684 Map \u5bf9\u8c61\u63d0\u4f9b\u4e86\u5b58\u50a8\u7684\u96c6\u5408\uff0c\u5b83\u662f\u4e00\u79cd\u66f4\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6bd4\u4f20\u7edf\u7684\u5bf9\u8c61\u66f4\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u521b\u5efa Map \u5bf9\u8c61<\/strong><\/p>\n<p>\u8981\u521b\u5efa Map \u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>const myMap = new Map();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6dfb\u52a0\u952e\u503c\u5bf9<\/strong><\/p>\n<p>\u8981\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u5230 Map \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 set() \u65b9\u6cd5\uff1a<\/p>\n<pre>myMap.set('key1', 'value1');\n\/\/ \u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u952e\u503c\u5bf9\nmyMap.set('key2', 'value2');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u83b7\u53d6\u503c<\/strong><\/p>\n<p>\u8981\u83b7\u53d6\u4e0e\u7279\u5b9a\u952e\u5173\u8054\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 get() \u65b9\u6cd5\uff1a<\/p>\n<pre>const value = myMap.get('key1'); \/\/ \"value1\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728<\/strong><\/p>\n<p>\u8981\u68c0\u67e5 Map \u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528 has() \u65b9\u6cd5\uff1a<\/p>\n<pre>const hasKey = myMap.has('key1'); \/\/ true<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5220\u9664\u952e\u503c\u5bf9<\/strong><\/p>\n<p>\u8981\u4ece Map \u4e2d\u5220\u9664\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528 delete() \u65b9\u6cd5\uff1a<\/p>\n<pre>myMap.delete('key1');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u904d\u5386 Map<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 forEach() \u65b9\u6cd5\u904d\u5386 Map \u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\uff1a<\/p>\n<pre>myMap.forEach((value, key) =&gt; {\n  console.log(`Key: ${key}, Value: ${value}`);\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u52bf<\/strong><\/p>\n<p>\u4f7f\u7528 Map \u7684\u4f18\u52bf\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>\u952e\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\uff1a<\/strong>Map \u7684\u952e\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5bf9\u8c61\u6216\u5176\u4ed6\u4efb\u4f55\u7c7b\u578b\u3002<\/li>\n<li> <strong>\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\uff1a<\/strong>Map \u7684\u503c\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002<\/li>\n<li> <strong>\u5feb\u901f\u67e5\u627e\uff1a<\/strong>Map \u4f7f\u7528\u54c8\u5e0c\u8868\u5b58\u50a8\u952e\uff0c\u8fd9\u4f7f\u5f97\u67e5\u627e\u64cd\u4f5c\u975e\u5e38\u9ad8\u6548\u3002<\/li>\n<li> <strong>\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff1a<\/strong>Map \u4e2d\u7684\u952e\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5b58map\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u63d0\u4f9b\u4e86 map \u5bf9\u8c61\u6765\u5b58\u50a8\u952e\u503c\u5bf9\uff0c\u5b83\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u67e5\u627e\u548c\u904d\u5386\u529f\u80fd\uff0c\u4e0e\u4f20\u7edf\u5bf9\u8c61\u76f8\u6bd4\u66f4\u9002\u5408\u5904\u7406\u590d\u6742\u6570\u636e\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u5b58\u50a8 Map JavaScript \u4e2d\u7684 Map \u5bf9\u8c61\u63d0\u4f9b\u4e86\u5b58\u50a8\u7684\u96c6\u5408\uff0c\u5b83\u662f\u4e00\u79cd\u66f4\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6bd4\u4f20\u7edf\u7684\u5bf9\u8c61\u66f4\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u3002 \u521b\u5efa Map \u5bf9\u8c61 \u8981\u521b\u5efa Map \u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a const myMap = new Map(); \u767b\u5f55\u540e\u590d\u5236 \u6dfb\u52a0\u952e\u503c\u5bf9 \u8981\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u5230 Map \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 set() \u65b9\u6cd5\uff1a myMap.set(&#8216;key1&#8217;, &#8216;value1&#8217;); \/\/ \u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u952e\u503c\u5bf9 myMap.set(&#8216;key2&#8217;, &#8216;value2&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u83b7\u53d6\u503c \u8981\u83b7\u53d6\u4e0e\u7279\u5b9a\u952e\u5173\u8054\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 get() \u65b9\u6cd5\uff1a const value = myMap.get(&#8216;key1&#8217;); \/\/ &#8220;value1&#8221; \u767b\u5f55\u540e\u590d\u5236 \u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728 \u8981\u68c0\u67e5 Map \u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528 has() \u65b9\u6cd5\uff1a const [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3449","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3449"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3449\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}