{"id":5528,"date":"2023-01-05T12:56:50","date_gmt":"2023-01-05T04:56:50","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5528"},"modified":"2023-01-05T12:56:50","modified_gmt":"2023-01-05T04:56:50","slug":"%e5%88%a9%e7%94%a8typescriptmap%e5%b0%87map%e5%b0%8d%e8%b1%a1%e7%9a%84%e9%8d%b5%e5%90%8d%e4%bd%9c%e7%82%ba%e5%80%bc","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%88%a9%e7%94%a8typescriptmap%e5%b0%87map%e5%b0%8d%e8%b1%a1%e7%9a%84%e9%8d%b5%e5%90%8d%e4%bd%9c%e7%82%ba%e5%80%bc\/","title":{"rendered":"\u5229\u7528TypeScriptMap\u5c07Map\u5c0d\u8c61\u7684\u9375\u540d\u4f5c\u70ba\u503c"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, Map, KeysAsValues\"><\/p>\n<h1>\u4f7f\u7528 TypeScript Map \u5c07 Map \u5bf9\u8c61\u7684\u952e\u540d\u4f5c\u70ba\u503c(keysAsValues)<\/h1>\n<p>TypeScript Map \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5c07 Map \u5c0d\u8c61\u7684\u9375\u540d\u4f5c\u70ba\u503c\uff0c\u9019\u6a23\u53ef\u4ee5\u8b93\u4f60\u66f4\u5bb9\u6613\u5730\u64cd\u4f5c Map \u5c0d\u8c61\u3002<\/p>\n<p>\u5728 TypeScript \u4e2d\uff0cMap \u5c0d\u8c61\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5c07\u9375\u548c\u503c\u5c0d\u61c9\u8d77\u4f86\uff0c\u4e26\u4e14\u53ef\u4ee5\u8f15\u9b06\u5730\u67e5\u627e\u548c\u66f4\u65b0\u5c0d\u61c9\u7684\u503c\u3002<\/p>\n<p>\u4f7f\u7528 TypeScript Map \u7684 keysAsValues \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c07 Map \u5c0d\u8c61\u7684\u9375\u540d\u4f5c\u70ba\u503c\uff0c\u9019\u6a23\u53ef\u4ee5\u8b93\u4f60\u66f4\u5bb9\u6613\u5730\u64cd\u4f5c Map \u5c0d\u8c61\u3002<\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u4f60\u6709\u4e00\u500b Map \u5c0d\u8c61\uff0c\u5b83\u7684\u9375\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u662f\u6578\u5b57\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet myMap = new Map();\nmyMap.set(\"one\", 1);\nmyMap.set(\"two\", 2);\nmyMap.set(\"three\", 3);\n<\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u5c07 Map \u5c0d\u8c61\u7684\u9375\u540d\u4f5c\u70ba\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 TypeScript Map \u7684 keysAsValues \u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet myMapValues = myMap.keysAsValues();\n<\/pre>\n<p>\u9019\u6a23\uff0cmyMapValues \u5c31\u6703\u662f\u4e00\u500b\u65b0\u7684 Map \u5c0d\u8c61\uff0c\u5b83\u7684\u9375\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u662f\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre class=\"brush: javascript\">\nmyMapValues.get(\"one\"); \/\/ \"one\"\nmyMapValues.get(\"two\"); \/\/ \"two\"\nmyMapValues.get(\"three\"); \/\/ \"three\"\n<\/pre>\n<p>\u4f7f\u7528 TypeScript Map \u7684 keysAsValues \u65b9\u6cd5\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u5bb9\u6613\u5730\u64cd\u4f5c Map \u5c0d\u8c61\uff0c\u5c24\u5176\u662f\u7576\u4f60\u60f3\u8981\u5c07 Map \u5c0d\u8c61\u7684\u9375\u540d\u4f5c\u70ba\u503c\u6642\uff0c\u5b83\u5c31\u66f4\u52a0\u6709\u7528\u4e86\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u6b64\u5916\uff0cTypeScript Map \u9084\u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982 <span style=\"color: #009900; font-weight: bold;\">forEach()<\/span>\u3001<span style=\"color: #009900; font-weight: bold;\">map()<\/span> \u548c <span style=\"color: #009900; font-weight: bold;\">filter()<\/span>\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u5bb9\u6613\u5730\u64cd\u4f5c Map \u5c0d\u8c61\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <span style=\"color: #009900; font-weight: bold;\">forEach()<\/span> \u65b9\u6cd5\u4f86\u5c0d Map \u5c0d\u8c61\u4e2d\u7684\u6bcf\u500b\u9375\u548c\u503c\u9032\u884c\u64cd\u4f5c\uff1a<\/p>\n<pre class=\"brush: javascript\">\nmyMap.forEach((value, key) => {\n  console.log(`<span class=\"katex math inline\">{key}:<\/span>{value}`);\n});\n\n\/\/ one: 1\n\/\/ two: 2\n\/\/ three: 3\n<\/pre>\n<p>\u53e6\u5916\uff0c\u4f60\u9084\u53ef\u4ee5\u4f7f\u7528 <span style=\"color: #009900; font-weight: bold;\">map()<\/span> \u548c <span style=\"color: #009900; font-weight: bold;\">filter()<\/span> \u65b9\u6cd5\u4f86\u5c0d Map \u5c0d\u8c61\u4e2d\u7684\u6bcf\u500b\u9375\u548c\u503c\u9032\u884c\u64cd\u4f5c\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet myMapValues = myMap.map((value, key) => {\n  return `<span class=\"katex math inline\">{key}:<\/span>{value}`;\n});\n\n\/\/ [\"one: 1\", \"two: 2\", \"three: 3\"]\n\nlet myMapFiltered = myMap.filter((value, key) => {\n  return value > 1;\n});\n\n\/\/ [\"two\": 2, \"three\": 3]\n<\/pre>\n<p>\u7e3d\u4e4b\uff0cTypeScript Map \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u66f4\u5bb9\u6613\u5730\u64cd\u4f5c Map \u5c0d\u8c61\uff0c\u5c24\u5176\u662f\u7576\u4f60\u60f3\u8981\u5c07 Map \u5c0d\u8c61\u7684\u9375\u540d\u4f5c\u70ba\u503c\u6642\uff0c\u5b83\u5c31\u66f4\u52a0\u6709\u7528\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aTypeScriptMap\u662f\u4e00\u7a2e\u5c0d\u8c61\uff0c\u53ef\u4ee5\u5c07Map\u5c0d\u8c61\u7684\u9375\u540d\u4f5c\u70ba\u503c\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7c21\u55ae\u800c\u6709\u6548\u7684\u65b9\u6cd5\u4f86\u64cd\u4f5c\u548c\u7ba1\u7406\u8cc7\u6599\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScriptMap\u5c07Map\u5c0d\u8c61\u7684\u9375\u540d\u4f5c\u70ba\u503c\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4f86\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5528","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1ra","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5528"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5528\/revisions"}],"predecessor-version":[{"id":5529,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5528\/revisions\/5529"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}