{"id":3281,"date":"2022-04-23T13:17:32","date_gmt":"2022-04-23T05:17:32","guid":{"rendered":"http:\/\/www.yuguoxy.com\/geekhome\/?p=3281"},"modified":"2022-04-23T13:17:54","modified_gmt":"2022-04-23T05:17:54","slug":"%e6%95%b0%e7%bb%84reduce%e6%96%b9%e6%b3%95%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=3281","title":{"rendered":"\u6570\u7ec4reduce\u65b9\u6cd5\u8be6\u89e3"},"content":{"rendered":"<br \/>\n<h3>\u6570\u7ec4reduce()<\/h3>\n<p>  \u8bed\u6cd5:[java]reduce(callFun,[initValue])<br \/>\n      callFun: (previousValue,currentValue,currentIndex,arr)=&gt;{}<br \/>\n      initValue: previousValue\u521d\u59cb\u503c(\u53ef\u9009\u53c2\u6570)[\/java]<\/p>\n<p>         reduce() \u65b9\u6cd5\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6309\u5e8f\u6267\u884c\u4e00\u4e2a\u7531\u60a8\u63d0\u4f9b\u7684 callFun \u51fd\u6570\uff0c<br \/>\n               \u6bcf\u4e00\u6b21\u8fd0\u884c callFun \u4f1a\u5c06\u5148\u524d\u7684\u8ba1\u7b97\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u6700\u540e\u5c06\u5176\u7ed3\u679c\u6c47\u603b\u4e3a\u5355\u4e2a\u8fd4\u56de\u503c\u3002<\/p>\n<ul>\n<li> 1. \u904d\u5386\u6570\u7ec4\u5143\u7d20<\/li>\n<li> 2. \u6267\u884c\u51fd\u6570\u8c03\u7528\uff0c\u5c06callFun\u51fd\u6570\u8ba1\u7b97\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9previousValue<\/li>\n<li> 3. \u6700\u540e\u5c06\u5176\u7ed3\u679c\u6c47\u603b\u4e3a\u5355\u4e2a\u8fd4\u56de\u503c<\/li>\n<\/ul>\n<div id=\"sc_tips\">\u4e24\u79cd\u60c5\u51b5: initValue\u662f\u5426\u5b58\u5728<\/p>\n<p>              &#8211; yes:<br \/>\n                   previousValue \u521d\u59cb\u503c\u4e3ainitValue<br \/>\n                   currentValue \u521d\u59cb\u503c\u4e3aarr[0]<br \/>\n                   currentIndex \u521d\u59cb\u503c\u4e3a0<br \/>\n              &#8211; no\uff1a<br \/>\n                  previousValue \u521d\u59cb\u5316\u4e3a arr[0]<br \/>\n                  currentValue \u521d\u59cb\u503c\u4e3a arr[1]<br \/>\n                  currentIndex \u521d\u59cb\u503c\u4e3a 1<\/div>\n<blockquote><p>   \u5e94\u7528:<br \/>\n          &#8211; \u6c42\u6570\u7ec4\u6240\u6709\u503c\u7684\u548c<br \/>\n          &#8211; \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u5316\u4e3a\u4e00\u7ef4<br \/>\n          &#8211; \u8ba1\u7b97\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570<br \/>\n          &#8211; \u6570\u7ec4\u53bb\u91cd<\/p><\/blockquote>\n<p>[java]<br \/>\n\/**<br \/>\n * \u904d\u5386\u5143\u7d20<br \/>\n *    \u6709\u521d\u59cb\u503c\u60c5\u51b5<br \/>\n *\/<br \/>\n const test1 = () =&gt; {<br \/>\n\tlet arr = [4, 6, 7, 9]<br \/>\n\tarr.reduce((previousValue, currentValue, currentIndex) =&gt; {<br \/>\n\t\tconsole.log(`previousValue:${previousValue}, currentValue:${currentValue}, currentIndex:${currentIndex}`)<br \/>\n\t\treturn currentValue<br \/>\n\t}, 0)<br \/>\n}[\/java]<\/p>\n<p>[java]\/**<br \/>\n * \u904d\u5386\u5143\u7d20<br \/>\n *    \u65e0\u521d\u59cb\u503c\u60c5\u51b5<br \/>\n *\/<br \/>\nconst test2 = () =&gt; {<br \/>\n\tlet arr = [4, 6, 7, 9]<br \/>\n\tarr.reduce((previousValue, currentValue, currentIndex) =&gt; {<br \/>\n\t\tconsole.log(`previousValue:${previousValue}, currentValue:${currentValue}, currentIndex:${currentIndex}`)<br \/>\n\t\treturn currentValue<br \/>\n\t})<br \/>\n}[\/java]<\/p>\n<p>[java]\/**<br \/>\n * \u7d2f\u52a0\u6c42\u548c<br \/>\n *\/<br \/>\nconst test3 = () =&gt; {<br \/>\n\tlet arr = [4, 6, 7, 9]<br \/>\n\tlet sum = arr.reduce((previousValue, currentValue, currentIndex) =&gt; {<br \/>\n\t\tconsole.log(previousValue) \/\/0,4,10,17<br \/>\n\t\treturn previousValue + currentValue \/\/4,10,17,26<br \/>\n\t}, 0)<br \/>\n\tconsole.log(sum)<br \/>\n}[\/java]<\/p>\n<p>[java]\/**<br \/>\n * \u7d2f\u52a0\u6c42\u548c<br \/>\n *\/<br \/>\nconst test4 = () =&gt; {<br \/>\n\tlet arr = [<br \/>\n\t\t{ name: &#8216;jack&#8217;, score: 98 },<br \/>\n\t\t{ name: &#8216;rose&#8217;, score: 88 },<br \/>\n\t\t{ name: &#8216;tom&#8217;, score: 90 },<br \/>\n\t]<br \/>\n\t\/\/\u8ba1\u7b97\u603b\u6210\u7ee9<br \/>\n\tlet total = arr.reduce((previousValue, currentValue) =&gt; currentValue.score + previousValue, 0)<br \/>\n\tconsole.log(total)<br \/>\n}[\/java]<\/p>\n<p>[java]\/**<br \/>\n * \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u5316\u4e3a\u4e00\u7ef4<br \/>\n *\/<br \/>\nconst test5 = () =&gt; {<br \/>\n\tconst arr = [<br \/>\n\t\t[1, 2],<br \/>\n\t\t[3, 4],<br \/>\n\t\t[5, 6],<br \/>\n\t]<br \/>\n\tconst newArr = arr.reduce((previousValue, currentValue) =&gt; [&#8230;previousValue, &#8230;currentValue], [])<br \/>\n\tconsole.log(newArr)<br \/>\n}[\/java]<\/p>\n<p>[java]\/**<br \/>\n * \u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570<br \/>\n *   \u5224\u65ad\u5143\u7d20\u662f\u5426\u5728\u5bf9\u8c61\u4e2d: \u5bf9\u8c61[\u5143\u7d20]<br \/>\n *    in\u8bed\u53e5: \u5143\u7d20 in \u5bf9\u8c61<br \/>\n *<br \/>\n *\/<br \/>\nconst test6 = () =&gt; {<br \/>\n\tconst str = &#8216;helloworldjavascript&#8217;<br \/>\n\tconst arr = str.split(&#8221;)<br \/>\n\tconst obj = arr.reduce(<br \/>\n\t\t(previousValue, currentValue) =&gt;{<br \/>\n\t\t\t\/\/ previousValue[currentValue] ? previousValue[currentValue]++ : (previousValue[currentValue] = 1)<br \/>\n\t\t\tcurrentValue in previousValue ? previousValue[currentValue]++ : (previousValue[currentValue] = 1)<br \/>\n\t\t\treturn previousValue<br \/>\n\t\t},{})<br \/>\n\tconsole.log(obj)<br \/>\n}[\/java]<\/p>\n<p>[java]\/**<br \/>\n * \u6570\u7ec4\u53bb\u91cd<br \/>\n *\/<br \/>\nconst test7 = ()=&gt;{<br \/>\n\tconst arr = [1,2,3,1,2,4,5,3]<br \/>\n\tconst newArr = arr.reduce((previousValue,currentValue)=&gt;{<br \/>\n\t\tif(previousValue.indexOf(currentValue) == -1){<br \/>\n\t\t\tpreviousValue.push(currentValue)<br \/>\n\t\t}<br \/>\n\t\treturn previousValue<br \/>\n\t},[])<br \/>\n\tconsole.log(newArr)<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>[java]\/**<br \/>\n * \u68c0\u67e5\u6570\u636e\u7c7b\u578b<br \/>\n *  \u5bf9\u8c61Object<br \/>\n *  \u6570\u7ec4Array<br \/>\n *  \u51fd\u6570Function<br \/>\n *\/<br \/>\n const test8 = () =&gt; {<br \/>\n\t\/\/ let callback = ()=&gt;{}  \/\/ [object Function]<br \/>\n\t\/\/ let callback = {}      \/\/ [object Object]<br \/>\n\t\/\/ let callback = []      \/\/ [object Array]<br \/>\n\t\/\/ let callback = &#8221;      \/\/ [object String]<br \/>\n\t\/\/ let callback = 0       \/\/ [object Number]<br \/>\n\tlet callback = null \/\/ [object Null]<br \/>\n\tlet value = Object.prototype.toString.call(callback)<br \/>\n\tconsole.log(value)<br \/>\n}<br \/>\n[\/java]<br \/>\n[java]\/**<br \/>\n * \u81ea\u5b9a\u4e49reduce<br \/>\n *\/<br \/>\nArray.prototype.myReduce = function (callback, init) {<br \/>\n\t\/\/\u5224\u65adcallback\u662f\u5426\u51fd\u6570<br \/>\n\tif (!callback &amp;&amp; Object.prototype.toString.call(callback) !== &#8216;[object Function]&#8217;) {<br \/>\n\t\tthrow new TypeError(&#8216;callback is not Function&#8217;)<br \/>\n\t}<br \/>\n\t\/\/\u5224\u65ad\u65b9\u6cd5\u8c03\u7528\u8005\u662f\u5426\u6570\u7ec4<br \/>\n\tif (Object.prototype.toString.call(this) !== &#8216;[object Array]&#8217;) {<br \/>\n\t\tthrow new TypeError(&#8216;not Array&#8217;)<br \/>\n\t}<br \/>\n\t\/\/\u6570\u7ec4\u4e0d\u80fd\u4e3a\u7a7a<br \/>\n\tif (this.length === 0 &amp;&amp; init === undefined) {<br \/>\n\t\tthrow new TypeError(&#8216;Reduce of empty arr with no inital value&#8217;)<br \/>\n\t}<br \/>\n\tlet index = init === undefined ? 1 : 0 \/\/\u7d22\u5f15\u53f7<br \/>\n\tlet previousValue = init === undefined ? this[0] : init<br \/>\n\tconst len = this.length<\/p>\n<p>\t\/\/reduce\u529f\u80fd: \u904d\u5386\u6570\u7ec4\u5143\u7d20,\u6bcf\u6b21\u6267\u884c\u7ed3\u679c\u8fd4\u56de\u7ed9previousValue,\u5faa\u73af\u5b8c\u8fd4\u56depreviousValue<br \/>\n\twhile (index &lt; len) {<br \/>\n\t\tconst currentValue = this[index]<br \/>\n\t\tpreviousValue = callback(previousValue, currentValue, index, this)<br \/>\n\t\tindex++<br \/>\n\t}<br \/>\n\treturn previousValue<br \/>\n}[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u7ec4reduce() \u8bed\u6cd5:[java]reduce(callFun,[initValue]) callFun [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[109,105],"tags":[158],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3281"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3281"}],"version-history":[{"count":37,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3281\/revisions"}],"predecessor-version":[{"id":3318,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3281\/revisions\/3318"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}