{"id":53343,"date":"2024-12-03T15:50:22","date_gmt":"2024-12-03T07:50:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53343\/"},"modified":"2024-12-03T15:50:22","modified_gmt":"2024-12-03T07:50:22","slug":"%e6%8e%8c%e6%8f%a1-javascript-%e7%9a%84%e6%95%b0%e7%bb%84%e5%bc%ba%e5%a4%a7%e5%8a%9f%e8%83%bd%ef%bc%9aforeach%e3%80%81map%e3%80%81filter%e3%80%81reduce%e3%80%81spread-%e5%92%8c-rest","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53343\/","title":{"rendered":"\u638c\u63e1 JavaScript \u7684\u6570\u7ec4\u5f3a\u5927\u529f\u80fd\uff1aforEach\u3001map\u3001filter\u3001reduce\u3001spread \u548c rest"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u638c\u63e1 JavaScript \u7684\u6570\u7ec4\u5f3a\u5927\u529f\u80fd\uff1aforEach\u3001map\u3001filter\u3001reduce\u3001spread \u548c rest<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u638c\u63e1 JavaScript \u7684\u6570\u7ec4\u5f3a\u5927\u529f\u80fd\uff1aforEach\u3001map\u3001filter\u3001reduce\u3001spread \u548c rest\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/173286473367496adda42b4.jpg\" class=\"aligncenter\" title=\"\u638c\u63e1 JavaScript \u7684\u6570\u7ec4\u5f3a\u5927\u529f\u80fd\uff1aforEach\u3001map\u3001filter\u3001reduce\u3001spread \u548c rest\u63d2\u56fe\" alt=\"\u638c\u63e1 JavaScript \u7684\u6570\u7ec4\u5f3a\u5927\u529f\u80fd\uff1aforEach\u3001map\u3001filter\u3001reduce\u3001spread \u548c rest\u63d2\u56fe\" \/><\/p>\n<p><strong>forEach\uff1a\u8fed\u4ee3\u5143\u7d20<\/strong><\/p>\n<p>forEach \u65b9\u6cd5\u8fed\u4ee3\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4e3a\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u63d0\u4f9b\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>`const \u6570\u5b57 = [1, 2, 3, 4, 5];<\/p>\n<p>numbers.forEach(num =&gt; {<br \/> console.log(num);<br \/> });<br \/> `<\/p>\n<p><strong>\u5730\u56fe\uff1a\u53d8\u6362\u5143\u7d20<\/strong><\/p>\n<p>map \u65b9\u6cd5\u901a\u8fc7\u5c06\u63d0\u4f9b\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u539f\u59cb\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002<\/p>\n<p>`const \u6570\u5b57 = [1, 2, 3, 4, 5];<br \/> const doubledNumbers = Numbers.map(num =&gt; num * 2);<\/p>\n<p>console.log(doubledNumbers); \/\/ \u8f93\u51fa\uff1a[2, &nbsp; <br \/> 4, 6, 8, 10]<br \/> `<\/p>\n<p><strong>\u8fc7\u6ee4\u5668\uff1a\u9009\u62e9\u5143\u7d20<\/strong><\/p>\n<p>filter \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u4ec5\u5305\u542b\u901a\u8fc7\u6240\u63d0\u4f9b\u51fd\u6570\u5b9e\u73b0\u7684\u6d4b\u8bd5\u7684\u5143\u7d20\u3002<\/p>\n<p>`const \u6570\u5b57 = [1, 2, 3, 4, 5];<br \/> const EvenNumbers = Numbers.filter(num &nbsp; <br \/> =&gt; num % 2 === 0);<\/p>\n<p>console.log(evenNumbers); \/\/ \u8f93\u51fa\uff1a &nbsp; <br \/> [2, 4]`<\/p>\n<p><strong>\u51cf\u5c11\uff1a\u7d2f\u79ef\u503c<\/strong><\/p>\n<p>reduce \u65b9\u6cd5\u901a\u8fc7\u5bf9\u7d2f\u52a0\u5668\u548c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u51fd\u6570\uff0c\u5c06\u6570\u7ec4\u7f29\u51cf\u4e3a\u5355\u4e2a\u503c\u3002<\/p>\n<p>`const \u6570\u5b57 = [1, 2, 3, 4, 5];<br \/> const sum = Numbers.reduce((accumulator, currentValue) =&gt; Accumulator currentValue, 0);<\/p>\n<p>console.log(sum); \/\/ \u8f93\u51fa\uff1a15 &nbsp; `<\/p>\n<p>**<br \/> \u6269\u5c55\u8fd0\u7b97\u7b26 (&#8230;)\uff1a\u6269\u5c55\u5143\u7d20**<\/p>\n<p>\u5c55\u5f00\u8fd0\u7b97\u7b26\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u5bf9\u8c61\uff09\u6269\u5c55\u4e3a\u5176\u5404\u4e2a\u5143\u7d20\u3002<\/p>\n<p>`const \u6570\u5b57 = [1, 2, 3];<br \/> const newArray = [&#8230;\u6570\u5b57, 4, 5];<\/p>\n<p>console.log(newArray); \/\/ \u8f93\u51fa\uff1a[1, 2, 3, 4, 5]<br \/> \u8c28\u614e\u4f7f\u7528\u4ee3\u7801\u3002`<\/p>\n<p>*<em>\u4f11\u606f\u8fd0\u7b97\u7b26\uff08&#8230;\uff09\uff1a\u6536\u96c6\u5143\u7d20<br \/> *<\/em><br \/> \u5269\u4f59\u8fd0\u7b97\u7b26\u5c06\u5269\u4f59\u5143\u7d20\u6536\u96c6\u5230\u6570\u7ec4\u4e2d\u3002<br \/> `<br \/> \u51fd\u6570 sum(&#8230;\u6570\u5b57) {<br \/> returnnumbers.reduce((total, num) =&gt;total num, 0);<br \/> }<\/p>\n<p>console.log(sum(1, 2, 3, 4)); \/\/ \u8f93\u51fa\uff1a10`<\/p>\n<p><em>\u5b9e\u9645\u4f8b\u5b50\uff1a<\/em><\/p>\n<ol>\n<li>\u8fc7\u6ee4\u5076\u6570\uff1a<\/li>\n<\/ol>\n<p>const \u6570\u5b57 = [1, 2, 3, 4, 5, 6];<br \/> const EvenNumbers = Numbers.filter(num =&gt; num % 2 === 0);<br \/> console.log(\u5076\u6570); \/\/ \u8f93\u51fa\uff1a[2, 4, 6]<\/p>\n<ol>\n<li>\u7528\u5e73\u65b9\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff1a<\/li>\n<\/ol>\n<p>const \u6570\u5b57 = [1, 2, 3, 4, 5];<br \/> const squaredNumbers = Numbers.map(num =&gt; num * num);<br \/> console.log(squaredNumbers); \/\/ \u8f93\u51fa\uff1a[1, 4, 9, &nbsp; <br \/> 16, 25]<\/p>\n<ol>\n<li>\u5bf9\u6570\u7ec4\u5143\u7d20\u6c42\u548c\uff1a<\/li>\n<\/ol>\n<p>const \u6570\u5b57 = [1, 2, 3, 4, 5];<br \/> const sum = Numbers.reduce((accumulator, currentValue) =&gt; Accumulator currentValue, 0);<br \/> \u63a7\u5236\u53f0.log(\u603b\u548c); \/\/ \u8f93\u51fa\uff1a15 <\/p>\n<ol>\n<li>\u5c55\u5e73\u5d4c\u5957\u6570\u7ec4\uff1a<\/li>\n<\/ol>\n<p>constnestedArray = [[1, 2], [3, 4], [5]];<br \/> const flattenedArray =nestedArray.flat();<br \/> \u63a7\u5236\u53f0.log(flattenedArray); \/\/ \u8f93\u51fa\uff1a[1, 2, 3, 4, 5]<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u638c\u63e1 JavaScript \u7684\u6570\u7ec4\u5f3a\u5927\u529f\u80fd\uff1aforEach\u3001map\u3001filter\u3001reduce\u3001spread \u548c rest\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   .NET Core \u9879\u76ee\u8fc1\u79fb\u5230\u963f\u91cc\u4e91 RDS MySQL\uff0c\u53ea\u4fee\u6539\u8fde\u63a5\u5b57\u7b26\u4e32\u5c31\u591f\u4e86\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1 JavaScript \u7684\u6570\u7ec4\u5f3a\u5927\u529f\u80fd\uff1aforEach\u3001map\u3001filter\u3001reduce\u3001spread \u548c rest \u6536\u85cf \u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u638c\u63e1 JavaScript \u7684\u6570\u7ec4\u5f3a\u5927\u529f\u80fd\uff1aforEach\u3001map\u3001filter\u3001reduce\u3001spread \u548c rest\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01 forEach\uff1a\u8fed\u4ee3\u5143\u7d20 forEach \u65b9\u6cd5\u8fed\u4ee3\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4e3a\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u63d0\u4f9b\u7684\u56de\u8c03\u51fd\u6570\u3002 `const \u6570\u5b57 = [1, 2, 3, 4, 5]; numbers.forEach(num =&gt; { console.log(num); }); ` \u5730\u56fe\uff1a\u53d8\u6362\u5143\u7d20 map \u65b9\u6cd5\u901a\u8fc7\u5c06\u63d0\u4f9b\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u539f\u59cb\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002 `const \u6570\u5b57 = [1, 2, 3, 4, 5]; const doubledNumbers = Numbers.map(num =&gt; num * 2); console.log(doubledNumbers); \/\/ \u8f93\u51fa\uff1a[2, &nbsp; 4, 6, 8, 10] [&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-53343","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53343","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=53343"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53343\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}