{"id":3461,"date":"2024-11-10T13:40:04","date_gmt":"2024-11-10T05:40:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3461\/"},"modified":"2024-11-10T13:40:04","modified_gmt":"2024-11-10T05:40:04","slug":"js%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4hashmap","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3461\/","title":{"rendered":"js\u5982\u4f55\u5220\u9664hashmap"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u4f7f\u7528 javascript \u5220\u9664 hashmap\uff1f 1. \u4f7f\u7528 delete \u64cd\u4f5c\u7b26\u5220\u9664\u5355\u4e2a\u952e\u503c\u5bf9\uff1b 2. \u4f7f\u7528 clear() \u65b9\u6cd5\u5220\u9664\u6240\u6709\u952e\u503c\u5bf9\uff1b 3. \u4f7f\u7528 foreach() \u65b9\u6cd5\u8fed\u4ee3\u5e76\u5220\u9664\u7279\u5b9a\u952e\u503c\u5bf9\uff1b 4. \u4f7f\u7528 filter() \u65b9\u6cd5\u521b\u5efa\u4e0d\u5305\u542b\u7279\u5b9a\u952e\u503c\u5bf9\u7684\u65b0 hashmap\uff1b 5. \u4f7f\u7528 array.from() \u548c reduce() \u65b9\u6cd5\u5c06 hashmap \u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u518d\u5220\u9664\u7279\u5b9a\u952e\u503c\u5bf9\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110812485183930.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5220\u9664hashmap\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5220\u9664hashmap\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u5220\u9664 HashMap<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>HashMap \u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e JavaScript \u4e2d\u5b58\u50a8\u7684\u6570\u636e\u7ed3\u6784\u3002\u6709\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5220\u9664 HashMap \u4e2d\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u952e\u53ca\u5176\u5173\u8054\u7684\u503c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728 JavaScript \u4e2d\u5220\u9664 HashMap \u6761\u76ee\u7684\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u65b9\u6cd5<\/strong><\/p>\n<p><strong>1. \u4f7f\u7528 delete \u64cd\u4f5c\u7b26<\/strong><\/p>\n<p>delete \u64cd\u4f5c\u7b26\u53ef\u7528\u4e8e\u5220\u9664 HashMap \u4e2d\u7684\u5355\u4e2a\u952e\u503c\u5bf9\uff1a<\/p>\n<pre>const myMap = new Map();\nmyMap.set('key1', 'value1');\n\ndelete myMap.key1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 clear() \u65b9\u6cd5<\/strong><\/p>\n<p>clear() \u65b9\u6cd5\u53ef\u7528\u4e8e\u4ece HashMap \u4e2d\u5220\u9664\u6240\u6709\u952e\u503c\u5bf9\uff1a<\/p>\n<pre>const myMap = new Map();\nmyMap.set('key1', 'value1');\nmyMap.set('key2', 'value2');\n\nmyMap.clear();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 forEach() \u65b9\u6cd5<\/strong><\/p>\n<p>forEach() \u65b9\u6cd5\u53ef\u7528\u4e8e\u8fed\u4ee3 HashMap \u4e2d\u7684\u952e\u503c\u5bf9\uff0c\u5e76\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u5c06\u5b83\u4eec\u5220\u9664\uff1a<\/p>\n<pre>const myMap = new Map();\nmyMap.set('key1', 'value1');\nmyMap.set('key2', 'value2');\n\nmyMap.forEach((value, key) =&gt; {\n  if (key === 'key1') {\n    myMap.delete(key);\n  }\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 filter() \u65b9\u6cd5<\/strong><\/p>\n<p>filter() \u65b9\u6cd5\u53ef\u7528\u4e8e\u521b\u5efa HashMap \u7684\u65b0\u526f\u672c\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u952e\u503c\u5bf9\uff1a<\/p>\n<pre>const myMap = new Map();\nmyMap.set('key1', 'value1');\nmyMap.set('key2', 'value2');\n\nconst newMap = myMap.filter((value, key) =&gt; key !== 'key1');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4f7f\u7528 Array.from() \u548c reduce() \u65b9\u6cd5<\/strong><\/p>\n<p>Array.from() \u65b9\u6cd5\u53ef\u7528\u4e8e\u5c06 HashMap \u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 reduce() \u65b9\u6cd5\u4ece\u8be5\u6570\u7ec4\u4e2d\u5220\u9664\u7279\u5b9a\u952e\u503c\u5bf9\uff1a<\/p>\n<pre>const myMap = new Map();\nmyMap.set('key1', 'value1');\nmyMap.set('key2', 'value2');\n\nconst newMap = Array.from(myMap).reduce((acc, [key, value]) =&gt; {\n  if (key !== 'key1') {\n    acc.set(key, value);\n  }\n  return acc;\n}, new Map());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5220\u9664hashmap\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>\u5982\u4f55\u4f7f\u7528 javascript \u5220\u9664 hashmap\uff1f 1. \u4f7f\u7528 delete \u64cd\u4f5c\u7b26\u5220\u9664\u5355\u4e2a\u952e\u503c\u5bf9\uff1b 2. \u4f7f\u7528 clear() \u65b9\u6cd5\u5220\u9664\u6240\u6709\u952e\u503c\u5bf9\uff1b 3. \u4f7f\u7528 foreach() \u65b9\u6cd5\u8fed\u4ee3\u5e76\u5220\u9664\u7279\u5b9a\u952e\u503c\u5bf9\uff1b 4. \u4f7f\u7528 filter() \u65b9\u6cd5\u521b\u5efa\u4e0d\u5305\u542b\u7279\u5b9a\u952e\u503c\u5bf9\u7684\u65b0 hashmap\uff1b 5. \u4f7f\u7528 array.from() \u548c reduce() \u65b9\u6cd5\u5c06 hashmap \u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u518d\u5220\u9664\u7279\u5b9a\u952e\u503c\u5bf9\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u5220\u9664 HashMap \u7b80\u4ecb HashMap \u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e JavaScript \u4e2d\u5b58\u50a8\u7684\u6570\u636e\u7ed3\u6784\u3002\u6709\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5220\u9664 HashMap \u4e2d\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u952e\u53ca\u5176\u5173\u8054\u7684\u503c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5728 JavaScript \u4e2d\u5220\u9664 HashMap \u6761\u76ee\u7684\u51e0\u79cd\u65b9\u6cd5\u3002 \u65b9\u6cd5 1. \u4f7f\u7528 delete \u64cd\u4f5c\u7b26 delete \u64cd\u4f5c\u7b26\u53ef\u7528\u4e8e\u5220\u9664 HashMap \u4e2d\u7684\u5355\u4e2a\u952e\u503c\u5bf9\uff1a const myMap [&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-3461","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3461","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=3461"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3461\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}