{"id":872,"date":"2022-02-23T11:34:35","date_gmt":"2022-02-23T03:34:35","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=872"},"modified":"2022-02-23T11:35:21","modified_gmt":"2022-02-23T03:35:21","slug":"reduce-qiu-dui-xiang-shu-zu-de-he","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=872","title":{"rendered":"reduce \u6c42\u5bf9\u8c61\u6570\u7ec4\u7684\u548c"},"content":{"rendered":"<p>reduce \u8ba1\u7b97\u6570\u5b57\u6570\u7ec4\u4e4b\u548c\u5f88\u65b9\u4fbf\uff1a<\/p>\n<pre><code class=\"language-plain_text\">let sum = [0, 1, 2, 3].reduce(function (previousValue, currentValue) {\n  return previousValue + currentValue\n})\n\/\/ sum is 6\n<\/code><\/pre>\n<p>\u4f46\u82e5\u662f\u5bf9\u8c61\u6570\u7ec4\uff1a<\/p>\n<pre><code class=\"language-plain_text\">    var data = [\n        {\n            &quot;label&quot;: &quot;\u78b1\u5de5\u6bb5&quot;,\n            &quot;value&quot;: 0\n        },\n        {\n            &quot;label&quot;: &quot;\u6c2f\u5de5\u533a&quot;,\n            &quot;value&quot;: 0\n        },\n        {\n            &quot;label&quot;: &quot;\u4e59\u7094\u5de5\u6bb5&quot;,\n            &quot;value&quot;: 0\n        },\n        {\n            &quot;label&quot;: &quot;\u6c2f\u4e59\u70ef\u5de5\u6bb5&quot;,\n            &quot;value&quot;: 0\n        },\n        {\n            &quot;label&quot;: &quot;\u805a\u5408\u5de5\u533a&quot;,\n            &quot;value&quot;: 0\n        },\n        {\n            &quot;label&quot;: &quot;\u516c\u7528\u5de5\u6bb5&quot;,\n            &quot;value&quot;: 0\n        },\n        {\n            &quot;label&quot;: &quot;\u8bbe\u5907\u7ba1\u7406\u5de5\u6bb5&quot;,\n            &quot;value&quot;: 0\n        },\n        {\n            &quot;label&quot;: &quot;\u6210\u54c1\u5de5\u6bb5&quot;,\n            &quot;value&quot;: 0\n        }\n    ]\n<\/code><\/pre>\n<p>\u82e5\u6b64\u65f6\u8fd8\u7528\u4e0a\u9762\u76f4\u63a5\u5199\u4f1a\u5f97\u5230\uff1anull\u3002\u6b63\u786e\u505a\u6cd5\u9700\u8981\u7a0d\u5fae\u5904\u7406\u4e00\u4e0b\uff1a<\/p>\n<pre><code class=\"language-plain_text\">var total = data.reduce((prevValue,currentValue)=&gt;{\n    return prevValue + currentValue.value\n},0)\n<\/code><\/pre>\n<p>\u5173\u952e\u662f\uff1a<\/p>\n<ol>\n<li>\u521d\u59cb\u503c0\uff0c<\/li>\n<li>\u5199\u6210 preValue\uff0c\u800c\u975e preValue.value<\/li>\n<\/ol>\n<p>\u5b98\u65b9\u6587\u6863\u5df2\u7ecf\u6307\u51fa\uff1a<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/reduce#sum_of_values_in_an_object_array\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/reduce#sum_of_values_in_an_object_array<\/a><\/p>\n<p>StackOverflow \u4e0a\u4e5f\u6709\u4e00\u4e2a\u76f8\u5173\u7684\u63d0\u95ee\uff1a<\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/5732043\/how-to-call-reduce-on-an-array-of-objects-to-sum-their-properties\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/5732043\/how-to-call-reduce-on-an-array-of-objects-to-sum-their-properties<\/a><\/p>\n<p>\u5176\u4e2d\u4e24\u4e2a\u65b9\u6cd5\u5f88\u6709\u610f\u601d\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/02\/16455870251616.jpg\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/02\/16455870353162.jpg\" alt=\"\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u9700\u8981\u6784\u9020\u6210\u548c\u539f\u5355\u4e2a\u6570\u7ec4\u91cc\u7684\u5bf9\u8c61\u76f8\u540c\u7684\u7ed3\u6784\u5c31\u6210\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>reduce \u8ba1\u7b97\u6570\u5b57\u6570\u7ec4\u4e4b\u548c\u5f88\u65b9\u4fbf\uff1a let sum = [0, 1, 2, 3].reduce(&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[65],"class_list":["post-872","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-reduce"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/872","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=872"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/872\/revisions"}],"predecessor-version":[{"id":873,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/872\/revisions\/873"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}