{"id":3185,"date":"2024-11-10T09:49:20","date_gmt":"2024-11-10T01:49:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3185\/"},"modified":"2024-11-10T09:49:20","modified_gmt":"2024-11-10T01:49:20","slug":"js-%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3185\/","title":{"rendered":"js \u5982\u4f55\u904d\u5386\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\uff1afor \u5faa\u73af\uff1a\u6700\u901a\u7528\uff0c\u53ef\u63a7\u5236\u987a\u5e8f\u548c\u7ec8\u6b62\u3002for&#8230;of \u5faa\u73af\uff1a\u66f4\u7b80\u6d01\uff0c\u4f46\u4e0d\u80fd\u63a7\u5236\u987a\u5e8f\u3002foreach \u65b9\u6cd5\uff1a\u7b80\u5355\u6613\u7528\uff0c\u4f46\u4e0d\u80fd\u63a7\u5236\u987a\u5e8f\u3002map \u65b9\u6cd5\uff1a\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u9002\u7528\u4e8e\u5bf9\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002reduce \u65b9\u6cd5\uff1a\u7d2f\u79ef\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u8ba1\u7b97\u603b\u548c\u7b49\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110806302376640.jpg\" class=\"aligncenter\" title=\"js \u5982\u4f55\u904d\u5386\u6570\u7ec4\u63d2\u56fe\" alt=\"js \u5982\u4f55\u904d\u5386\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u4e2d\u904d\u5386\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\u904d\u5386\u6570\u7ec4\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\uff1a<\/p>\n<p><strong>1. for \u5faa\u73af<\/strong><\/p>\n<pre>const arr = [1, 2, 3, 4, 5];\n\nfor (let i = 0; i &lt; arr.length; i++) {\n  console.log(arr[i]);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. for&#8230;of \u5faa\u73af<\/strong><\/p>\n<pre>for (const elem of arr) {\n  console.log(elem);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. forEach \u65b9\u6cd5<\/strong><\/p>\n<pre>arr.forEach((elem) =&gt; {\n  console.log(elem);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. map \u65b9\u6cd5<\/strong><\/p>\n<pre>const newArr = arr.map((elem) =&gt; {\n  return elem * 2;\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. reduce \u65b9\u6cd5<\/strong><\/p>\n<pre>const sum = arr.reduce((acc, cur) =&gt; {\n  return acc + cur;\n}, 0);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0d\u540c\u904d\u5386\u65b9\u6cd5\u7684\u6027\u80fd\u548c\u9002\u7528\u573a\u666f\u6709\u6240\u4e0d\u540c\uff1a<\/p>\n<ul>\n<li> <strong>for \u5faa\u73af<\/strong>\uff1a\u6700\u901a\u7528\uff0c\u53ef\u63a7\u5236\u904d\u5386\u7684\u987a\u5e8f\u548c\u7ec8\u6b62\u6761\u4ef6\u3002<\/li>\n<li> <strong>for&#8230;of \u5faa\u73af<\/strong>\uff1a\u66f4\u7b80\u6d01\uff0c\u4e0d\u80fd\u63a7\u5236\u904d\u5386\u7684\u987a\u5e8f\u3002<\/li>\n<li> <strong>forEach \u65b9\u6cd5<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u4e0d\u80fd\u63a7\u5236\u904d\u5386\u7684\u987a\u5e8f\u3002<\/li>\n<li> <strong>map \u65b9\u6cd5<\/strong>\uff1a\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u9002\u7528\u4e8e\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<li> <strong>reduce \u65b9\u6cd5<\/strong>\uff1a\u7d2f\u79ef\u6570\u7ec4\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u8ba1\u7b97\u603b\u548c\u3001\u5e73\u5747\u503c\u7b49\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs \u5982\u4f55\u904d\u5386\u6570\u7ec4\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>\u5728 javascript \u4e2d\u904d\u5386\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\uff1afor \u5faa\u73af\uff1a\u6700\u901a\u7528\uff0c\u53ef\u63a7\u5236\u987a\u5e8f\u548c\u7ec8\u6b62\u3002for&#8230;of \u5faa\u73af\uff1a\u66f4\u7b80\u6d01\uff0c\u4f46\u4e0d\u80fd\u63a7\u5236\u987a\u5e8f\u3002foreach \u65b9\u6cd5\uff1a\u7b80\u5355\u6613\u7528\uff0c\u4f46\u4e0d\u80fd\u63a7\u5236\u987a\u5e8f\u3002map \u65b9\u6cd5\uff1a\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u9002\u7528\u4e8e\u5bf9\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002reduce \u65b9\u6cd5\uff1a\u7d2f\u79ef\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u8ba1\u7b97\u603b\u548c\u7b49\u3002 \u5982\u4f55\u5728 JavaScript \u4e2d\u904d\u5386\u6570\u7ec4 \u5728 JavaScript \u4e2d\u904d\u5386\u6570\u7ec4\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\uff1a 1. for \u5faa\u73af const arr = [1, 2, 3, 4, 5]; for (let i = 0; i &lt; arr.length; i++) { console.log(arr[i]); } \u767b\u5f55\u540e\u590d\u5236 2. for&#8230;of \u5faa\u73af for (const elem of arr) { console.log(elem); } \u767b\u5f55\u540e\u590d\u5236 3. forEach \u65b9\u6cd5 arr.forEach((elem) =&gt; [&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-3185","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3185","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=3185"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3185\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}