{"id":1335,"date":"2022-06-27T11:38:23","date_gmt":"2022-06-27T03:38:23","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1335"},"modified":"2022-06-29T23:17:53","modified_gmt":"2022-06-29T15:17:53","slug":"45-46-array-map-array-reduce","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1335","title":{"rendered":"45 &#8211; 46 &#8211; 47 &#8211; Array Map &#038; Array Reduce &#038; Array Filter"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16563011197508.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/array-map-explained-6de\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/array-map-explained-6de<\/a><\/p>\n<p><code>map()<\/code> \u65b9\u6cd5\u4e3a\u6bcf\u4e00\u4e2a\u6570\u7ec4\u7684\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\u65b0\u7684\u6570\u7ec4\u548c\u539f\u6570\u7ec4\u7684\u957f\u5ea6\u4e00\u6837\u3002<code>map()<\/code> \u7684\u56de\u8c03\u51fd\u6570\u63a5\u53d7\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4ee3\u8868\u5f53\u524d\u5904\u7406\u7684\u5143\u7d20\u3002<\/p>\n<p>\u5f53\u4f60\u9700\u8981\u4e3a\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b <code>map()<\/code> \u5f88\u6709\u5e2e\u52a9\u3002\u4f60\u53ef\u80fd\u4f1a\u7ed9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u53cc\u500d\u5904\u7406\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const arr = [2, 4, 9, 22];\nconst map = arr.map(x =&gt; x * 2);\nconsole.log(map); \/\/ [ 4, 8, 18, 44 ]\n<\/code><\/pre>\n<p>\u4f60\u4f7f\u7528 map \u540c\u6837\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u6570\u7ec4\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u56de\u8c03\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e0b\u6807\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const arr = [2, 4, 9, 22];\nconst map = arr.map((value, index) =&gt; value * index); \/\/ multiplying array value with its index\nconsole.log(map); \/\/ [ 0, 4, 18, 66 ]\n<\/code><\/pre>\n<p>\u82e5\u83b7\u53d6\u4e0b\u6807\u8fd8\u4e0d\u591f\uff0c\u4f60\u8fd8\u53ef\u4ee5\u83b7\u53d6\u5230\u539f\u6570\u7ec4\u3002<\/p>\n<h2><a id=\"map%E6%96%B9%E6%B3%95%E5%92%8C-this%E5%85%B3%E9%94%AE%E5%AD%97\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>map \u65b9\u6cd5\u548c this \u5173\u952e\u5b57<\/h2>\n<p>\u6709\u65f6\u5019\u4f60\u9700\u8981\u4e3a <code>map()<\/code> \u6307\u5b9a <code>this<\/code> \u4e0a\u4e0b\u6587\uff0c<code>map<\/code> \u652f\u6301\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u5165 <code>this<\/code>\u3002<\/p>\n<pre><code class=\"language-plain_text\">const util = {\n    firstName: &quot;John&quot;,\n    lastNames: [&quot;Wick&quot;, &quot;Malcolm&quot;, &quot;Smith&quot;],\n    randomNameGenerator: function () {\n        const newNames = this.lastNames.map(function (surname) {\n            return (`${this.firstName} ${surname}`); \/\/ we can access first name provided by this object\n        }, this); \/\/ passing reference to this object\n        return newNames;\n    }\n}\n\nconsole.log(util.randomNameGenerator()); \/\/ [ 'John Wick', 'John Malcolm', 'John Smith' ]\n<\/code><\/pre>\n<h2><a id=\"%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E4%B8%8D%E4%BD%BF%E7%94%A8map\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4ec0\u4e48\u65f6\u5019\u4e0d\u4f7f\u7528 map<\/h2>\n<ul>\n<li>\u82e5\u4f60\u4e0d\u6253\u7b97\u4f7f\u7528\u8fd4\u56de\u7684\u65b0\u6570\u7ec4\u6216\u56de\u8c03\u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u5219\u4e0d\u8981\u4f7f\u7528 <code>map<\/code>\u3002\u82e5\u4f60\u60f3\u66f4\u65b0\u539f\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>forEach<\/code> \u6216 <code>for...of<\/code>\u3002<\/li>\n<li>\u4e0d\u8981\u548c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ea\u4f20\u9012\u4e00\u4e2a\u53c2\u6570\u4f46\u53ef\u4ee5\u63a5\u6536\u66f4\u591a\u53c2\u6570\u7684\u5185\u7f6e\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p>\u6211\u6765\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const arr = [&quot;2&quot;, &quot;4&quot;, &quot;9&quot;];\nconst map = arr.map(parseInt);\nconsole.log(map);\n<\/code><\/pre>\n<p>\u4f60\u53ef\u80fd\u671f\u671b\u7ed3\u679c\u4e3a <code>[2,4,9]<\/code>\uff0c\u6bcf\u4e2a\u5143\u7d20\u901a\u8fc7 <code>parseInt<\/code> \u5904\u7406\u540e\u5f97\u5230\u6570\u5b57\u7c7b\u578b\u3002\u4f60\u4f1a\u60ca\u8bb6\u7684\u53d1\u73b0\u7ed3\u679c\u4e3a\uff1a<code>[2,NaN,NaN]<\/code>\u3002\u8bb0\u4f4f <code>parseInt<\/code> \u63a5\u6536\u4e86\u4e09\u4e2a\u53c2\u6570\uff1aelement,index \u548c array\u3002\u6240\u4ee5\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a\u5143\u7d20\uff0cindex \u662f 1 \u548c 2 \u5bfc\u81f4 <code>parseInt<\/code> \u8fd4\u56de NaN\u3002<\/p>\n<hr \/>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16563438405349.jpg\" alt=\"\" \/><\/p>\n<p><code>reduce()<\/code> \u65b9\u6cd5\u4e3a\u8f93\u5165\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528 reducer \u51fd\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u53ef\u4f20\u5165\u6570\u7ec4\u3001\u7d2f\u52a0\u5668\u3001\u5f53\u524d\u503c\u548c\u4e0b\u6807\u4f5c\u4e3a\u53c2\u6570\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>\u5f53\u4f60\u9700\u8981\u4e3a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u65f6 <code>reduce()<\/code> \u4f1a\u5f88\u6709\u7528\u3002\u4f60\u6216\u8bb8\u9700\u8981\u628a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u76f8\u52a0\u3002<\/p>\n<pre><code class=\"language-plain_text\">const arr = [2, 4, 9, 22];\nconst reduce = arr.reduce((final, current) =&gt; final + current);\nconsole.log(reduce); \/\/ 37 (2 + 4 + 9 + 22)\n<\/code><\/pre>\n<p>final \u5c31\u662f\u6700\u521d\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u76f8\u52a0\u7684\u7d2f\u52a0\u5668\uff0ccurrent \u4ee3\u8868\u5f53\u524d\u5faa\u73af\u7684\u503c\u3002<\/p>\n<p>\u4f60\u540c\u6837\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u6570\u7ec4\u5faa\u73af\u7684\u4e0b\u6807\uff0c\u56de\u8c03\u51fd\u6570\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f5c\u4e3a\u4e0b\u6807\u3002<\/p>\n<pre><code class=\"language-plain_text\">const arr = [2, 4, 9, 22];\n\/\/ we will add all numbers as well as their indices\nconst reduce = arr.reduce((final, current, index) =&gt; final + current + index);\nconsole.log(reduce); \/\/ 43 (2 + 0 + 4 + 1 + 9 + 2 + 22 + 3)\n<\/code><\/pre>\n<p>\u5982\u679c\u83b7\u53d6\u4e0b\u6807\u8fd8\u4e0d\u591f\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4ece\u7b2c\u56db\u4e2a\u53c2\u6570\u83b7\u53d6\u539f\u59cb\u6570\u7ec4\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u518d\u4e3e\u4e00\u4e2a\u4f8b\u5b50\u6765\u770b\u770b reduce \u7684\u5f3a\u5927\u4e4b\u5904\u4ee5\u53ca\u4f7f\u7528\u7684\u573a\u666f\uff0c\u6211\u4eec\u5c06\u4f1a\u4e0d\u501f\u52a9 <code>flat<\/code> \u65b9\u6cd5\u6765\u4f7f\u6570\u7ec4\u5c55\u5f00\u3002<\/p>\n<pre><code class=\"language-plain_text\">const flatArray = (arr) =&gt; {\n    let output = [];\n    return arr.reduce((final, value) =&gt; {\n        \/\/ use recursion, if value then concat to our final array else call flatArray again with the child array\n        return final.concat(Array.isArray(value) ? flatArray(value) : value);\n    }, output);\n}\n\nconst input = [1, 2, 3, [10, 11, 12], 21, 22, 23, [31, 32, 33, 34], [41, 42]];\nconsole.log(flatArray(input)); \/\/ [ 1, 2, 3, 10, 11, 12, 21, 22, 23, 31, 32, 33, 34, 41, 42 ]\n<\/code><\/pre>\n<p><code>reduce<\/code> \u6709\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f46\u662f\u5b83\u5e76\u4e0d\u50cf <code>map<\/code> \u4e00\u6837\u662f <code>this<\/code> \u4e0a\u4e0b\u6587\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u800c\u662f <code>reduce<\/code> \u5f00\u59cb\u7684\u521d\u59cb\u503c\u3002<\/p>\n<p>\u770b\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\u6211\u4eec\u628a <code>output<\/code> \u4f5c\u4e3a\u521d\u59cb\u503c\u3002\u82e5\u6ca1\u6709\u63d0\u4f9b\u521d\u59cb\u503c\uff0c\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5c06\u4f5c\u4e3a\u521d\u59cb\u503c\uff0c\u6570\u7ec4\u5c06\u4ece\u7b2c\u4e8c\u4e2a\u5143\u7d20\u5f00\u59cb\u5faa\u73af\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16564203574166.jpg\" alt=\"\" \/><\/p>\n<p><code>filter()<\/code> \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u7531\u6ee1\u8db3\u6d4b\u8bd5\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20\u7ec4\u6210\u7684\u65b0\u6570\u7ec4\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u652f\u6301\u8f93\u5165\u5f53\u524d\u5143\u7d20\u3001\u4e0b\u6807\u548c\u539f\u59cb\u6570\u7ec4\u7684\u56de\u8c03\u51fd\u6570\u3002\u6700\u540e\u4e24\u4e2a\u53c2\u6570\uff08\u4e0b\u6807\u548c\u6570\u7ec4\uff09\u662f\u53ef\u9009\u7684\u3002<\/p>\n<p>\u5f53\u4f60\u9700\u8981\u4ece\u8f93\u5165\u7684\u6570\u7ec4\u4e2d\u6311\u9009\u51fa\u6ee1\u8db3\u4f60\u6807\u51c6\u7684\u5143\u7d20\u65f6 <code>filter()<\/code> \u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u6216\u8bb8\u4f60\u53ef\u80fd\u60f3\u8981\u6570\u7ec4\u4e2d\u6240\u6709\u7684\u5076\u6570\u6216\u5b57\u7b26\u4e32\u957f\u5ea6\u5927\u4e8e 6 \u7684\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-plain_text\">const names = [&quot;Parwinder&quot;, &quot;Leah&quot;, &quot;Lauren&quot;, &quot;Eliu&quot;, &quot;Robert&quot;, &quot;George&quot;, &quot;Eric&quot;];\nconst output = names.filter(name =&gt; name.length &gt;= 6);\n\nconsole.log(output); \/\/ [ 'Parwinder', 'Lauren', 'Robert', 'George' ]\nconsole.log(names); \/\/ [ 'Parwinder', 'Leah', 'Lauren', 'Eliu', 'Robert', 'George', 'Eric' ]\n<\/code><\/pre>\n<p><code>filter()<\/code> \u5e76\u4e0d\u6539\u53d8\u539f\u59cb\u6570\u7ec4\uff0c\u5c31\u50cf\u4e0a\u9762\u7684\u4f8b\u5b50\u4e00\u6837\u539f\u59cb\u6570\u7ec4\u4e0d\u4f1a\u6539\u53d8\u3002<\/p>\n<p>\u5728 <code>filter()<\/code> \u65b9\u6cd5\u4e2d\u540c\u6837\u53ef\u4ee5\u83b7\u53d6\u6570\u7ec4\u7684\u4e0b\u6807\uff0c\u56de\u8c03\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u4e0b\u6807\u3002<\/p>\n<pre><code class=\"language-plain_text\">const arr = [1, 2, 4, 9, 22, 75, 16];\nconst filter = arr.filter((current, index) =&gt; (current % index === 0));\n\/\/ return values that are divisible by the index they are on\nconsole.log(filter); \/\/ [ 2, 4, 9, 75 ]\n<\/code><\/pre>\n<p>\u82e5\u4e0b\u6807\u8fd8\u4e0d\u591f\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7b2c\u4e09\u4e2a\u53c2\u6570\u83b7\u53d6\u539f\u59cb\u6570\u7ec4\u3002<\/p>\n<p><code>filter()<\/code> \u8fd8\u6709\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a<code>this<\/code>\u3002\u5b83\u6b63\u50cf <code>map()<\/code> \u65b9\u6cd5\u4e00\u6837\uff0c\u6307\u5b9a\u4e86\u56de\u8c03\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/array-map-expl&#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":[167,65],"class_list":["post-1335","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-map","tag-reduce"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1335","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=1335"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1335\/revisions"}],"predecessor-version":[{"id":1341,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1335\/revisions\/1341"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}