{"id":333001,"date":"2021-02-07T03:49:55","date_gmt":"2021-02-06T19:49:55","guid":{"rendered":"http:\/\/4563.org\/?p=333001"},"modified":"2021-02-07T03:49:55","modified_gmt":"2021-02-06T19:49:55","slug":"%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%aa-reduce-%e6%96%b9%e6%b3%95%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=333001","title":{"rendered":"\u8bf7\u6559\u4e00\u4e2a reduce \u65b9\u6cd5\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e00\u4e2a reduce \u65b9\u6cd5\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : teslayun <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5728\u67e5\u9605 MDN \u6587\u6863\u7684\u65f6\u5019\u770b\u5230\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<p>\u5982\u679c\u6570\u7ec4\u4e3a\u7a7a\u4e14\u6ca1\u6709\u63d0\u4f9b initialValue\uff0c\u4f1a\u629b\u51fa TypeError \u3002\u5982\u679c\u6570\u7ec4\u4ec5\u6709\u4e00\u4e2a\u5143\u7d20\uff08\u65e0\u8bba\u4f4d\u7f6e\u5982\u4f55\uff09\u5e76\u4e14\u6ca1\u6709\u63d0\u4f9b initialValue\uff0c \u6216\u8005\u6709\u63d0\u4f9b initialValue \u4f46\u662f\u6570\u7ec4\u4e3a\u7a7a\uff0c\u90a3\u4e48\u6b64\u552f\u4e00\u503c\u5c06\u88ab\u8fd4\u56de\u5e76\u4e14 callback \u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<p>\u63d0\u4f9b\u521d\u59cb\u503c\u901a\u5e38\u66f4\u5b89\u5168\uff0c\u6b63\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u5982\u679c\u6ca1\u6709\u63d0\u4f9b initialValue\uff0c\u5219\u53ef\u80fd\u6709\u56db\u79cd\u8f93\u51fa\uff1a<\/p>\n<pre><code>var maxCallback = ( acc, cur ) =&gt; Math.max( acc.x, cur.x ); var maxCallback2 = ( max, cur ) =&gt; Math.max( max, cur );  \/\/ reduce() \u6ca1\u6709\u521d\u59cb\u503c [ { x: 2 }, { x: 22 }, { x: 42 } ].reduce( maxCallback ); \/\/ NaN [ { x: 2 }, { x: 22 }            ].reduce( maxCallback ); \/\/ 22 [ { x: 2 }                       ].reduce( maxCallback ); \/\/ { x: 2 } [                                ].reduce( maxCallback ); \/\/ TypeError  \/\/ map\/reduce; \u8fd9\u662f\u66f4\u597d\u7684\u65b9\u6848\uff0c\u5373\u4f7f\u4f20\u5165\u7a7a\u6570\u7ec4\u6216\u66f4\u5927\u6570\u7ec4\u4e5f\u53ef\u6b63\u5e38\u6267\u884c [ { x: 22 }, { x: 42 } ].map( el =&gt; el.x )                         .reduce( maxCallback2, -Infinity ); <\/code><\/pre>\n<p>\u8fd9\u91cc\u9762\u7b2c\u4e00\u4e2a\u6570\u7ec4\u7684 reduce \u4e3a\u5565\u4f1a\u8fd4\u56de NaN \u5462\uff0c\u6c42\u5927\u4f6c\u89e3\u60d1\u3002 \u5730\u5740\uff1a https:\/\/developer.mozilla.org\/zh-cn\/docs\/web\/javascript\/reference\/global_objects\/array\/reduce<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5235571\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bearice <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Math.max(undefined,anything) == NaN<br \/>Math.max(NaN,anything) == NaN                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5235572\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liuy1994g <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             isNaN(Math.max(undefined, 2)) === true                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5235573\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Austaras <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             maxCallback \u8fd4\u56de\u7684\u662f\u6570\u5b57\uff0c\u6570\u5b57\u4e0a\u6ca1\u6709 \u2018x\u2019 \u8fd9\u4e2a\u5c5e\u6027                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5235574\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SakuraKuma <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Math.max \u8fd4\u56de\u7684\u662f Number, \u6ca1\u6709.x                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5235575\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yyfearth <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             reduce \u6709\u4e24\u4e2a\u53c2\u8d5b \u7b2c\u4e00\u4e2a\u662f\u4e00\u4e2a function \u5c31\u548c\u4f60\u73b0\u5728\u7528\u7684\u4e00\u6837 \u4f46\u662f\u6709\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5c31\u662f\u521d\u59cb\u503c<br \/>\u5982\u679c\u521d\u59cb\u503c\u6ca1\u7ed9 \u90a3\u4e48\u7b2c\u4e00\u4e2a\u5143\u7d20\u5c31\u662f\u521d\u59cb\u503c<br \/>\u6240\u4ee5 [ { x: 2 }, { x: 22 }, { x: 42 } ].reduce( maxCallback ) \u5c31\u662f [ { x: 22 }, { x: 42 } ].reduce( maxCallback, { x: 2 } )<br \/>\u7b2c\u4e00\u6b21\u8fd0\u7b97 \u662f { x: 2 } \u548c { x: 22 } \u7ed3\u679c\u4e3a 22<br \/>\u7b2c\u4e8c\u6b21\u8fd0\u7b97 \u662f 22 \u548c { x: 42 } \u90a3\u4e48 (22).x \u662f undefined \u90a3\u4e48 Math.max(undefined, 42) \u7ed3\u679c\u5c31\u662f NaN<\/p>\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6b63\u786e\u7684\u505a\u6cd5\u5148\u505a\u4e86\u4e00\u4e0b map \u628a x \u90fd\u53d6\u51fa\u6765 \u7136\u540e\u5728 reduce<br \/>\u4e0d\u8fc7\u6211\u8fd9\u6837\u5e94\u8be5\u662f\u66f4\u7b80\u5355\u7684\u505a\u6cd5 \u53ea\u4e0d\u8fc7\u8fd9\u4e2a function \u6ca1\u6709\u666e\u904d\u6027 \u5176\u4ed6\u5730\u65b9\u53ef\u80fd\u6ca1\u529e\u6cd5\u590d\u7528\u903b\u8f91<br \/>[ { x: 22 }, { x: 42 } ].reduce( ( acc, cur ) =&gt; Math.max( acc, cur.x ), -Infinity)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5235576\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : momocraft <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ece\u7b2c 2 \u5757\u7684\u7b2c 3 \u884c\u5411\u4e0a\u770b<br \/>\u5f53 acc \u662f 22 \u7684\u65f6\u5019 \uff0c\u518d\u548c {x:42} reduce \u4e00\u6b21\u5c31\u5f97\u5230 NaN                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5235577\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : teslayun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u8c22\u4e0a\u51e0\u4f4d\u5927\u4f6c\u89e3\u60d1\uff0c\u660e\u767d\u4e86\uff0creduce \u91cc\u9762\u7684\u51fd\u6570\u56de\u8c03\u7b2c\u4e09\u6b21\u6267\u884c\u7684\u65f6\u5019 maxCallback \u8fd4\u56de\u7684\u6570\u5b57\u6ca1\u6709 X \u5c5e\u6027\uff1a\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5235578\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xngiser <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             var maxCallback = ( acc, cur ) =&gt; Math.max( acc.x, cur.x );<\/p>\n<p>\u7b2c\u4e00\u6b21 acc\uff1a { x: 2 } cur\uff1a { x: 22 } acc=&gt; 22<br \/>\u7b2c\u4e8c\u6b21 acc: 22 cur: { x: 42 } acc=&gt; NaN                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5235579\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SoloCompany <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7c7b\u578b\u4e0d\u5339\u914d\uff0c\u8981\u4e48\u5148 map \u518d reduce\uff0c\u8981\u4e48 reduce \u8fd4\u56de\u76f8\u540c\u5957\u5a03\u7ed3\u6784\u4f53                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5235580\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : KuroNekoFan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             reduce \u7684 api \u6709\u70b9\u590d\u6742\uff0c\u5176\u5b9e\u4f60\u5f04\u4e2a forEach \u4e5f\u884c\u7684\uff0c\u800c\u4e14\u6bd4\u8f83\u597d\u8bfb\uff0c\u5c31\u662f\u8981\u591a\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e00\u4e2a reduce \u65b9\u6cd5\u7684\u95ee\u9898&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/333001"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=333001"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/333001\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=333001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=333001"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=333001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}