{"id":52409,"date":"2024-12-03T14:31:39","date_gmt":"2024-12-03T06:31:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52409\/"},"modified":"2024-12-03T14:31:39","modified_gmt":"2024-12-03T06:31:39","slug":"javascript-%e4%b8%ad%e7%9a%84-map-%e5%92%8c-for-%e5%be%aa%e7%8e%af%e5%9c%a8%e5%a4%84%e7%90%86-promise-%e8%bf%94%e5%9b%9e%e5%80%bc%e6%97%b6%e6%9c%89%e4%bd%95%e5%8c%ba%e5%88%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52409\/","title":{"rendered":"JavaScript \u4e2d\u7684 `map` \u548c `for` \u5faa\u73af\u5728\u5904\u7406 Promise \u8fd4\u56de\u503c\u65f6\u6709\u4f55\u533a\u522b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>JavaScript \u4e2d\u7684 `map` \u548c `for` \u5faa\u73af\u5728\u5904\u7406 Promise \u8fd4\u56de\u503c\u65f6\u6709\u4f55\u533a\u522b\uff1f <\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aJavaScript \u4e2d\u7684 `map` \u548c `for` \u5faa\u73af\u5728\u5904\u7406 Promise \u8fd4\u56de\u503c\u65f6\u6709\u4f55\u533a\u522b\uff1f \u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732089120673d952076168.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684 `map` \u548c `for` \u5faa\u73af\u5728\u5904\u7406 Promise \u8fd4\u56de\u503c\u65f6\u6709\u4f55\u533a\u522b\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684 `map` \u548c `for` \u5faa\u73af\u5728\u5904\u7406 Promise \u8fd4\u56de\u503c\u65f6\u6709\u4f55\u533a\u522b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript \u4e2d for \u548c map \u8fd4\u56de promise \u65f6\u7684\u4e0d\u540c\u8868\u73b0<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 javascript \u4e2d\u7684 map \u65b9\u6cd5\u904d\u5386\u4e00\u4e2a\u6570\u7ec4\u65f6\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u539f\u59cb\u6570\u7ec4\u4e2d\u5143\u7d20\u7ecf\u8fc7\u56de\u8c03\u51fd\u6570\u7684\u5904\u7406\u7ed3\u679c\u3002\u800c for \u5faa\u73af\u4f1a\u9010\u4e2a\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u6267\u884c\u5faa\u73af\u4f53\u4e2d\u5b9a\u4e49\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u5f53\u4f60\u60f3\u8981\u8fd4\u56de\u4e00\u4e2a promise \u65f6\uff0c\u9700\u8981\u8003\u8651 map \u548c for \u7684\u4e0d\u540c\u884c\u4e3a\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u4f7f\u7528 map<\/strong>\uff1a<\/p>\n<p>\u5982\u679c\u5728 map \u56de\u8c03\u51fd\u6570\u5185\u90e8\u4f7f\u7528 return \u8bed\u53e5\uff0c\u8fd4\u56de\u7684\u503c\u4f1a\u4f20\u7ed9 map \u65b9\u6cd5\u5185\u90e8\u7684\u533f\u540d\u51fd\u6570\uff0c\u800c\u4e0d\u4f1a\u4f20\u7ed9\u5916\u90e8\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5728\u5916\u90e8\u51fd\u6570\u4e2d\u9700\u8981\u8bbf\u95ee map \u56de\u8c03\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5fc5\u987b\u663e\u5f0f\u5730\u5728 map \u56de\u8c03\u51fd\u6570\u4e2d\u8fd4\u56de promise\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 for<\/strong>\uff1a<\/p>\n<p>for \u5faa\u73af\u4e0d\u521b\u5efa\u65b0\u7684\u6570\u7ec4\uff0c\u5b83\u9010\u4e2a\u5904\u7406\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5982\u679c\u5728 for \u5faa\u73af\u4f53\u4e2d\u4f7f\u7528 return \u8bed\u53e5\uff0c\u5b83\u5c06\u7acb\u5373\u505c\u6b62\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u503c\u7ed9\u5916\u90e8\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 for \u5faa\u73af\u65f6\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728 for \u5faa\u73af\u4f53\u4e2d\u8fd4\u56de promise\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5728\u4f60\u7684\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 map \u7248\u672c\u8fd4\u56de undefined \u800c\u4e0d\u662f\u56fe\u7247\u6570\u636e\uff0c\u662f\u56e0\u4e3a result.map \u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u800c for \u5faa\u73af\u5bf9\u539f\u59cb\u6570\u7ec4\u8fdb\u884c\u9010\u4e2a\u5904\u7406\u3002\u4e3a\u4e86\u5728 map \u7248\u672c\u4e2d\u6b63\u786e\u8fd4\u56de\u56fe\u7247\u6570\u636e\uff0c\u53ef\u4ee5\u5c06\u56de\u8c03\u51fd\u6570\u7684\u8fd4\u56de\u503c\u8d4b\u7ed9 mappedresult \u53d8\u91cf\uff0c\u7136\u540e\u4f7f\u7528 find \u65b9\u6cd5\u8fd4\u56de\u6570\u7ec4\u4e2d\u7b2c\u4e00\u4e2a\u975e undefined \u7684\u5143\u7d20\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/ getCapture\u51fd\u6570\u5185\u90e8\nconst mappedResult = result.map(m =&gt; {\n    if (m.name === '\u6574\u4e2a\u5c4f\u5e55') {\n      console.log(m)\n      let str = m.thumbnail.crop({x: 0, y: 0, width: 1000, height: 1000})\n      const imgStr = str.toDataURL()\n      return imgStr\n    }\n    return undefined;\n });\n return mappedResult.find(imgStr =&gt; imgStr !== undefined);<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJavaScript \u4e2d\u7684 `map` \u548c `for` \u5faa\u73af\u5728\u5904\u7406 Promise \u8fd4\u56de\u503c\u65f6\u6709\u4f55\u533a\u522b\uff1f \u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   win10\u7cfb\u7edf\u600e\u4e48\u67e5\u770b\u7248\u672c\u7684\u4fe1\u606f win10\u67e5\u770b\u7248\u672c\u4fe1\u606f\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d\u7684 `map` \u548c `for` \u5faa\u73af\u5728\u5904\u7406 Promise \u8fd4\u56de\u503c\u65f6\u6709\u4f55\u533a\u522b\uff1f \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aJavaScript \u4e2d\u7684 `map` \u548c `for` \u5faa\u73af\u5728\u5904\u7406 Promise \u8fd4\u56de\u503c\u65f6\u6709\u4f55\u533a\u522b\uff1f \u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 javascript \u4e2d for \u548c map \u8fd4\u56de promise \u65f6\u7684\u4e0d\u540c\u8868\u73b0 \u5728\u4f7f\u7528 javascript \u4e2d\u7684 map \u65b9\u6cd5\u904d\u5386\u4e00\u4e2a\u6570\u7ec4\u65f6\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u539f\u59cb\u6570\u7ec4\u4e2d\u5143\u7d20\u7ecf\u8fc7\u56de\u8c03\u51fd\u6570\u7684\u5904\u7406\u7ed3\u679c\u3002\u800c for \u5faa\u73af\u4f1a\u9010\u4e2a\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u6267\u884c\u5faa\u73af\u4f53\u4e2d\u5b9a\u4e49\u7684\u4ee3\u7801\u3002 \u5f53\u4f60\u60f3\u8981\u8fd4\u56de\u4e00\u4e2a promise \u65f6\uff0c\u9700\u8981\u8003\u8651 map \u548c for \u7684\u4e0d\u540c\u884c\u4e3a\u3002 \u4f7f\u7528 map\uff1a \u5982\u679c\u5728 map \u56de\u8c03\u51fd\u6570\u5185\u90e8\u4f7f\u7528 return \u8bed\u53e5\uff0c\u8fd4\u56de\u7684\u503c\u4f1a\u4f20\u7ed9 map \u65b9\u6cd5\u5185\u90e8\u7684\u533f\u540d\u51fd\u6570\uff0c\u800c\u4e0d\u4f1a\u4f20\u7ed9\u5916\u90e8\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u5728\u5916\u90e8\u51fd\u6570\u4e2d\u9700\u8981\u8bbf\u95ee map \u56de\u8c03\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5fc5\u987b\u663e\u5f0f\u5730\u5728 map \u56de\u8c03\u51fd\u6570\u4e2d\u8fd4\u56de promise\u3002 \u4f7f\u7528 for\uff1a [&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-52409","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52409","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=52409"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52409\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}