{"id":5671,"date":"2023-01-06T14:31:23","date_gmt":"2023-01-06T06:31:23","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5671"},"modified":"2023-01-06T14:31:23","modified_gmt":"2023-01-06T06:31:23","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react-js%e7%9a%84reduce%e6%8a%98%e5%8f%a0%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react-js%e7%9a%84reduce%e6%8a%98%e5%8f%a0%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528React.JS\u7684Reduce\u6298\u53e0\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, React List, \u6298\u53e0, reduce\"><\/p>\n<h1>React List \u7684\u6298\u53e0(reduce)<\/h1>\n<p>React \u662f\u4e00\u500b\u7531 Facebook \u6240\u958b\u767c\u7684 JavaScript \u51fd\u5f0f\u5eab\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5efa\u7acb\u4e92\u52d5\u5f0f\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u3002React List \u7684\u6298\u53e0(reduce)\u662f React \u4e2d\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u4e00\u500b\u9663\u5217\u8f49\u63db\u6210\u4e00\u500b\u55ae\u4e00\u7684\u503c\u3002<\/p>\n<p><!--more--><\/p>\n<p>React List \u7684\u6298\u53e0(reduce)\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u4e00\u500b\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u7d93\u904e\u4e00\u500b\u51fd\u5f0f\u8655\u7406\uff0c\u6700\u5f8c\u5c07\u6240\u6709\u5143\u7d20\u8f49\u63db\u6210\u4e00\u500b\u55ae\u4e00\u7684\u503c\u3002\u5b83\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: javascript\">\narr.reduce(callback(accumulator, currentValue[, index[, array]]) [, initialValue])\n<\/pre>\n<p>\u5176\u4e2d\uff0c<code>callback<\/code> \u662f\u4e00\u500b\u51fd\u5f0f\uff0c\u5b83\u6703\u63a5\u6536\u56db\u500b\u53c3\u6578\uff1a<code>accumulator<\/code>\uff08\u7d2f\u52a0\u5668\uff09\u3001<code>currentValue<\/code>\uff08\u7576\u524d\u503c\uff09\u3001<code>index<\/code>\uff08\u7576\u524d\u7d22\u5f15\uff09\u548c <code>array<\/code>\uff08\u7576\u524d\u9663\u5217\uff09\u3002<code>initialValue<\/code> \u662f\u53ef\u9078\u53c3\u6578\uff0c\u7528\u65bc\u6307\u5b9a\u7d2f\u52a0\u5668\u7684\u521d\u59cb\u503c\u3002<\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u9663\u5217 <code>[1, 2, 3, 4]<\/code>\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 React List \u7684\u6298\u53e0(reduce)\u4f86\u5c07\u5b83\u8f49\u63db\u6210\u4e00\u500b\u55ae\u4e00\u7684\u503c\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst arr = [1, 2, 3, 4];\n\nconst sum = arr.reduce((acc, curr) => acc + curr, 0);\n\nconsole.log(sum); \/\/ 10\n<\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5c07 <code>initialValue<\/code> \u8a2d\u70ba 0\uff0c\u4e26\u5c07 <code>callback<\/code> \u8a2d\u70ba\u4e00\u500b\u51fd\u5f0f\uff0c\u5b83\u6703\u5c07 <code>accumulator<\/code> \u548c <code>currentValue<\/code> \u76f8\u52a0\uff0c\u6700\u5f8c\u5c07\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u76f8\u52a0\u5f8c\u8f49\u63db\u6210\u4e00\u500b\u55ae\u4e00\u7684\u503c\uff0c\u4e5f\u5c31\u662f 10\u3002<\/p>\n<p>React List \u7684\u6298\u53e0(reduce)\u529f\u80fd\u975e\u5e38\u5f37\u5927\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u4e00\u500b\u9663\u5217\u8f49\u63db\u6210\u4efb\u4f55\u5f62\u5f0f\u7684\u503c\uff0c\u4f8b\u5982\u5c07\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u76f8\u4e58\uff0c\u6216\u662f\u5c07\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8f49\u63db\u6210\u4e00\u500b\u65b0\u7684\u9663\u5217\u7b49\u7b49\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>React List \u7684\u6298\u53e0(reduce)\u662f React \u4e2d\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u4e00\u500b\u9663\u5217\u8f49\u63db\u6210\u4e00\u500b\u55ae\u4e00\u7684\u503c\u3002\u5b83\u7684\u8a9e\u6cd5\u70ba <code>arr.reduce(callback(accumulator, currentValue[, index[, array]]) [, initialValue])<\/code>\uff0c\u5176\u4e2d <code>callback<\/code> \u662f\u4e00\u500b\u51fd\u5f0f\uff0c\u5b83\u6703\u63a5\u6536\u56db\u500b\u53c3\u6578\uff1a<code>accumulator<\/code>\uff08\u7d2f\u52a0\u5668\uff09\u3001<code>currentValue<\/code>\uff08\u7576\u524d\u503c\uff09\u3001<code>index<\/code>\uff08\u7576\u524d\u7d22\u5f15\uff09\u548c <code>array<\/code>\uff08\u7576\u524d\u9663\u5217\uff09\uff0c<code>initialValue<\/code> \u662f\u53ef\u9078\u53c3\u6578\uff0c\u7528\u65bc\u6307\u5b9a\u7d2f\u52a0\u5668\u7684\u521d\u59cb\u503c\u3002React List \u7684\u6298\u53e0(reduce)\u529f\u80fd\u975e\u5e38\u5f37\u5927\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u4e00\u500b\u9663\u5217\u8f49\u63db\u6210\u4efb\u4f55\u5f62\u5f0f\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528React.JS\u7684Reduce\u6298\u53e0\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u5730\u5c07\u5217\u8868\u5143\u7d20\u8f49\u63db\u70ba\u55ae\u4e00\u503c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u7bc4\u4f8b\u4f86\u89e3\u91cb\u5982\u4f55\u4f7f\u7528Reduce\u6298\u53e0\u529f\u80fd\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":[184,183],"tags":[182,181],"class_list":["post-5671","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1tt","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5671","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=5671"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5671\/revisions"}],"predecessor-version":[{"id":5672,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5671\/revisions\/5672"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}