{"id":5396,"date":"2025-05-31T13:48:16","date_gmt":"2025-06-04T06:15:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5396"},"modified":"2025-06-04T13:48:16","modified_gmt":"2025-06-04T06:15:16","slug":"%e4%bd%bf%e7%94%a8typescriptmap%e5%b0%87map%e5%b0%8d%e8%b1%a1%e8%88%87%e5%87%bd%e6%95%b8%e5%90%88%e4%bd%b5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%bd%bf%e7%94%a8typescriptmap%e5%b0%87map%e5%b0%8d%e8%b1%a1%e8%88%87%e5%87%bd%e6%95%b8%e5%90%88%e4%bd%b5\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22 TypeScript \u4e2d\u7684 Map\uff1a\u5982\u4f55\u4f7f\u7528 mergeWithFunction() \u5408\u4f75\u6578\u64da\u8207\u51fd\u6578"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, Map, mergeWithFunction, JavaScript, \u6559\u5b78, \u6578\u64da\u7d50\u69cb\"><\/p>\n<h1>\u6df1\u5165\u63a2\u7d22 TypeScript \u4e2d\u7684 Map\uff1a\u5982\u4f55\u4f7f\u7528 mergeWithFunction() \u5408\u4f75\u6578\u64da\u8207\u51fd\u6578<\/h1>\n<p>\u5728 TypeScript \u4e2d\uff0cMap \u662f\u4e00\u7a2e\u5f37\u5927\u7684\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u7528\u4f86\u9ad8\u6548\u5730\u5b58\u5132\u548c\u64cd\u4f5c\u9375\u503c\u5c0d\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5229\u7528 TypeScript \u7684 Map \u4f86\u8207\u51fd\u6578\u5408\u4f75\uff0c\u5f9e\u800c\u63d0\u5347\u6578\u64da\u7ba1\u7406\u7684\u9748\u6d3b\u6027\u548c\u53ef\u64cd\u4f5c\u6027\u3002\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e `mergeWithFunction()` \u7684\u7528\u6cd5\uff0c\u8b93\u4f60\u5728\u5be6\u969b\u958b\u767c\u4e2d\u5f97\u5fc3\u61c9\u624b\u3002<\/p>\n<p>### \u4ec0\u9ebc\u662f TypeScript Map\uff1f<\/p>\n<p>TypeScript \u7684 Map \u5c0d\u8c61\u662f\u4e00\u7a2e\u9375\u503c\u5c0d\u96c6\u5408\uff0c\u5b83\u5141\u8a31\u6211\u5011\u4ee5\u4efb\u610f\u985e\u578b\u7684\u9375\u4f86\u5b58\u5132\u548c\u67e5\u627e\u5c0d\u61c9\u7684\u503c\u3002\u9019\u4f7f\u5f97 Map \u5728\u9700\u8981\u5feb\u901f\u67e5\u627e\u6642\u7279\u5225\u6709\u7528\u3002\u7136\u800c\uff0c\u6a19\u6e96\u7684 Map \u4e26\u4e0d\u652f\u6301\u5c07\u51fd\u6578\u76f4\u63a5\u8207\u9375\u503c\u5c0d\u7d50\u5408\uff0c\u9019\u5c31\u662f\u70ba\u4ec0\u9ebc `mergeWithFunction()` \u61c9\u904b\u800c\u751f\u3002<\/p>\n<p>### \u4ec0\u9ebc\u662f mergeWithFunction()\uff1f<\/p>\n<p>`mergeWithFunction()` \u662f\u4e00\u500b\u53ef\u4ee5\u5c07 Map \u5c0d\u8c61\u8207\u4e00\u500b\u51fd\u6578\u5408\u4f75\u7684\u65b9\u6cd5\u3002\u9019\u500b\u51fd\u6578\u63a5\u6536\u4e09\u500b\u53c3\u6578\uff1a\u9375\u3001\u820a\u7684\u503c\u548c\u65b0\u7684\u503c\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u5408\u4f75\u5f8c\u7684\u7d50\u679c\u3002\u9019\u4f7f\u5f97\u6211\u5011\u80fd\u5920\u5c0d Map \u4e2d\u7684\u6bcf\u4e00\u5c0d\u9375\u503c\u9032\u884c\u81ea\u5b9a\u7fa9\u7684\u64cd\u4f5c\u3002<\/p>\n<p>### \u5982\u4f55\u4f7f\u7528 mergeWithFunction()<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `mergeWithFunction()` \u4f86\u5408\u4f75 Map \u5c0d\u8c61\u548c\u51fd\u6578\uff1a<\/p>\n<p>&#8220;`typescript<br \/>\nlet myMap = new Map<string, number>();<br \/>\nmyMap.set(&#8220;Alice&#8221;, 10);<br \/>\nmyMap.set(&#8220;Bob&#8221;, 20);<br \/>\n&#8220;`<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u5e0c\u671b\u5c07 Map \u4e2d\u6bcf\u500b\u4eba\u7684\u5206\u6578\u7d2f\u52a0\uff0c\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 `mergeWithFunction()`\uff1a<\/p>\n<p>&#8220;`typescript<br \/>\nmyMap = new Map([&#8230;myMap.entries()].map(([key, value]) => [key, value + 5]));<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5c07\u6bcf\u500b\u4eba\u7684\u5206\u6578\u589e\u52a0\u4e86 5\u3002\u96d6\u7136\u9019\u500b\u7bc4\u4f8b\u6c92\u6709\u76f4\u63a5\u4f7f\u7528 `mergeWithFunction()`\uff0c\u5b83\u5c55\u793a\u4e86 Map \u7684\u9748\u6d3b\u6027\u3002\u5be6\u969b\u4e0a\uff0c`mergeWithFunction()` \u4e3b\u8981\u662f\u7528\u65bc\u66f4\u8907\u96dc\u7684\u5408\u4f75\u908f\u8f2f\u3002<\/p>\n<p>### \u4f7f\u7528\u5834\u666f<\/p>\n<p>\u4f7f\u7528 TypeScript \u7684 Map \u53ca `mergeWithFunction()`\uff0c\u6211\u5011\u53ef\u4ee5\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u6578\u64da\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; \u8a08\u7b97\u5404\u985e\u9805\u76ee\u7684\u7e3d\u548c<br \/>\n&#8211; \u5408\u4f75\u591a\u500b\u6578\u64da\u4f86\u6e90<br \/>\n&#8211; \u66f4\u65b0\u5df2\u6709\u6578\u64da\u800c\u4e0d\u4e1f\u5931\u91cd\u8981\u4fe1\u606f<\/p>\n<p>### \u7d50\u8ad6<\/p>\n<p>\u7e3d\u4e4b\uff0cTypeScript \u7684 Map \u8207 `mergeWithFunction()` \u63d0\u4f9b\u4e86\u4e00\u7a2e\u9748\u6d3b\u7684\u65b9\u5f0f\u4f86\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002\u7121\u8ad6\u4f60\u662f\u5728\u8655\u7406\u7528\u6236\u6578\u64da\u3001\u7d71\u8a08\u4fe1\u606f\u9084\u662f\u5176\u4ed6\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u638c\u63e1\u9019\u4e9b\u5de5\u5177\u5c07\u6975\u5927\u5730\u63d0\u5347\u4f60\u7684\u958b\u767c\u6548\u7387\u8207\u4ee3\u78bc\u8cea\u91cf\u3002<\/p>\n<p>\u73fe\u5728\u5c31\u958b\u59cb\u5be6\u8e10\uff0c\u5c07 `mergeWithFunction()` \u61c9\u7528\u5230\u4f60\u7684 TypeScript \u9805\u76ee\u4e2d\uff0c\u4eab\u53d7\u66f4\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u9ad4\u9a57\u5427\uff01<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aTypeScriptMap\u53ef\u4ee5\u5c07Map\u5c0d\u8c61\u8207\u51fd\u6578\u5408\u4f75\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u5c0dMap\u5c0d\u8c61\u9032\u884c\u64cd\u4f5c\uff0c\u66f4\u6709\u6548\u5730\u7ba1\u7406\u8cc7\u6599\u3002\u672c\u6587\u5c07\u4ecb\u7d39mergeWithFunction()\u51fd\u6578\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u5c07Map\u5c0d\u8c61\u8207\u51fd\u6578\u5408\u4f75\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-5396","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-1p2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5396","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=5396"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5396\/revisions"}],"predecessor-version":[{"id":5397,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5396\/revisions\/5397"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}