{"id":1129,"date":"2023-02-03T22:30:33","date_gmt":"2023-02-03T13:30:33","guid":{"rendered":"http:\/\/attacktube.com\/?p=1129"},"modified":"2024-01-08T12:18:35","modified_gmt":"2024-01-08T03:18:35","slug":"%e3%80%90javascript%e3%80%91array-prototype-reduce%e3%82%84%e3%82%84%e3%81%93%e3%81%97%e3%81%84%e3%81%aa%e9%80%94%e4%b8%ad%e3%81%a7%e6%96%ad%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/attacktube.com\/?p=1129","title":{"rendered":"\u3010JavaScript\u3011Array.prototype.reduce()"},"content":{"rendered":"\n<p>Array.prototype.reduce()\u306f\u3084\u3084\u3053\u3057\u3044\u306a\u3002<\/p>\n\n\n\n<p>\u7c21\u5358\u306a\u4f7f\u3044\u65b9<br>reduce\u30e1\u30bd\u30c3\u30c9\u3067\u914d\u5217\u306e\u4e2d\u306e\u6570\u5024\u3092\u3059\u3079\u3066\u8db3\u3057\u3066\u5408\u8a08\u5024\u3092\u6c42\u3081\u308b\u3002<br>reduce\u30e1\u30bd\u30c3\u30c9\u3067\u914d\u5217\u306e\u4e2d\u306e\u6587\u5b57\u5217\u3092\u3059\u3079\u3066\u9023\u7d50\u3057\u305f\u6587\u5b57\u5217\u3092\u4f5c\u308b\u3002<br>\u521d\u671f\u5024\u306b\u95a2\u3057\u3066\u306f\u3001\u7a7a\u306e\u914d\u5217\u3092\u521d\u671f\u5024\u306a\u3057\u3067\u5b9f\u884c\u3059\u308b\u3068\u30a8\u30e9\u30fc\u306b\u306a\u3063\u305f\u308a\u3001\u4e88\u671f\u305b\u306c\u5b9f\u884c\u7d50\u679c\u306b\u306a\u3063\u305f\u308a\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u521d\u671f\u5024\u306f\u8a2d\u5b9a\u3057\u305f\u65b9\u304c\u3044\u3044\u3002<br>\u914d\u5217\u306e\u4e2d\u306e\u6587\u5b57\u5217\u3092\u9023\u7d50\u3057\u305f\u3044\u306a\u3089join\u30e1\u30bd\u30c3\u30c9\u4f7f\u3063\u305f\u65b9\u304c\u8a18\u8ff0\u5185\u5bb9\u3082\u5c11\u306a\u304f\u3066\u5206\u304b\u308a\u3084\u3059\u3044\u306a\u3002<br><br>reduce (\u4ed6)\u3092\u6e1b\u3089\u3059<br><br>\u914d\u5217\u306e\u4e2d\u306e\u6570\u5024\u3092\u5168\u90e8\u5408\u8a08\u3059\u308b\u51e6\u7406\u306breduce\u30e1\u30bd\u30c3\u30c9\u4f7f\u3048\u308b\u306a\u3002<br>\u3057\u304b\u3057\u3001\u306a\u3093\u3067reduce\u3068\u3044\u3046\u540d\u524d\u306a\u3093\u3060\uff1f\u300c\u6e1b\u3089\u3059\u300d\u3068\u3044\u3046\u610f\u5473\u3060\u3088\u306a\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<br>\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-3461056110605997\" crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3461056110605997\" data-ad-slot=\"3137443461\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<br>\n\n\n\n<pre><code>\n\n    let numbers = [1,2,3,4,5,6,7,8,9,10];\n    let strings = ['\u3042\u3044\u3046\u3048\u304a','\u304b\u304d\u304f\u3051\u3053','\u3055\u3057\u3059\u305b\u305d'];\n    \n    let result = numbers.reduce(function(sum, element) {\n      return sum + element;\n    },0);\/\/ \u521d\u671f\u5024\u3068\u3057\u30660\u3092\u6307\u5b9a\n\n    let result2 = strings.reduce(function(sum, element) {\n      return sum + element;\n    },'');\/\/ \u521d\u671f\u5024\u3068\u3057\u3066\u7a7a\u306e\u6587\u5b57\u5217\u3092\u6307\u5b9a\n\n    console.log(result);\n    console.log(\"typeof result = \" + typeof result);\n    \n    console.log(result2);\n    console.log(\"typeof result2 = \" + typeof result2);\n    \n    let result3 = strings.join('');\n    \n    console.log(result3);\n    console.log(\"typeof result3 = \" + typeof result2);\n    \n\/*\n\n55\ntypeof result = number\n\u3042\u3044\u3046\u3048\u304a\u304b\u304d\u304f\u3051\u3053\u3055\u3057\u3059\u305b\u305d\ntypeof result2 = string\n\u3042\u3044\u3046\u3048\u304a\u304b\u304d\u304f\u3051\u3053\u3055\u3057\u3059\u305b\u305d\ntypeof result3 = string\n\n*\/\n\n<\/code><\/pre>\n\n\n\n<p>\u30a2\u30ed\u30fc\u95a2\u6570\u5f0f\u3082\u4f7f\u3048\u308b\u3002<\/p>\n\n\n\n<pre><code>\n\n    let numbers = [1,2,3,4,5,6,7,8,9,10];\n    let strings = ['\u3042\u3044\u3046\u3048\u304a','\u304b\u304d\u304f\u3051\u3053','\u3055\u3057\u3059\u305b\u305d'];\n\n    let result = numbers.reduce(( sum, element ) =&gt; sum + element,0);\n    console.log(result);\n    console.log(\"typeof result = \" + typeof result);\n    \n    \n    let result2 = strings.reduce(( sum, element ) =&gt; sum + element,\"\");\n    console.log(result2);\n    console.log(\"typeof result2 = \" + typeof result2);\n    \n\/*\n\n55\ntypeof result = number\n\u3042\u3044\u3046\u3048\u304a\u304b\u304d\u304f\u3051\u3053\u3055\u3057\u3059\u305b\u305d\ntypeof result2 = string\n\n*\/\n\n<\/code><\/pre>\n\n\n\n<p>\u4e0e\u3048\u3089\u308c\u305f\u914d\u5217names\u306e\u4e2d\u3067\u6700\u3082\u9577\u3044\u6587\u5b57\u5217\u3092\u898b\u3064\u3051\u308b\u65b9\u6cd5\u306freduce\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3068\u30b7\u30f3\u30d7\u30eb\u306b\u5b9f\u88c5\u3067\u304d\u308b\u3002<br><br>reduce\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3001\u30a2\u30ad\u30e5\u30e0\u30ec\u30fc\u30bf(accumulator)\u306eprev\u3068\u73fe\u5728\u306e\u5909\u6570current\u3092\u6bd4\u8f03\u3057\u3001<br>\u3088\u308a\u9577\u3044\u6587\u5b57\u5217\u3092\u4fdd\u6301\u3057\u3066\u3044\u304f\u3053\u3068\u3067\u3001\u6700\u7d42\u7684\u306a\u6700\u9577\u306e\u6587\u5b57\u5217\u3092\u898b\u3064\u3051\u308b\u3002<\/p>\n\n\n\n<p>\u30a2\u30ad\u30e5\u30e0\u30ec\u30fc\u30bf(accumulator)\u306f\u3001reduce\u30e1\u30bd\u30c3\u30c9\u5185\u3067\u4f7f\u7528\u3055\u308c\u308b\u5909\u6570\u3067\u3042\u308a\u3001<br>\u53cd\u5fa9\u51e6\u7406\u4e2d\u306b\u524d\u306e\u5024\u3092\u6307\u3059\u5909\u6570\u3067\u3082\u3042\u308b\u3002<br>reduce\u30e1\u30bd\u30c3\u30c9\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u5185\u3067\u306f\u3001\u30a2\u30ad\u30e5\u30e0\u30ec\u30fc\u30bf\u306e\u5024\u304c\u66f4\u65b0\u3055\u308c\u3001\u6b21\u306e\u53cd\u5fa9\u30b9\u30c6\u30c3\u30d7\u306b\u5f15\u304d\u7d99\u304c\u308c\u308b\u3002<\/p>\n\n\n\n<p>\u6700\u521d\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u306f\u3001\u521d\u671f\u5024\u3068\u3057\u3066\u7a7a\u306e\u6587\u5b57\u5217\u304cprev\u306b\u4ee3\u5165\u3055\u308c\u308b\u3002<br>\u305d\u308c\u4ee5\u964d\u306e\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u306f\u3001\u73fe\u5728\u306e\u5909\u6570current\u3068\u30a2\u30ad\u30e5\u30e0\u30ec\u30fc\u30bfprev\u306e\u5024\u3092\u6bd4\u8f03\u3057\u3001\u3088\u308a\u9577\u3044\u6587\u5b57\u5217\u3092prev\u306b\u683c\u7d0d\u3059\u308b\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u306e\u65b9\u6cd5\u306b\u3088\u308a\u3001\u914d\u5217names\u306e\u4e2d\u3067\u6700\u3082\u9577\u3044\u6587\u5b57\u5217\u3092\u6c42\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<pre><code>\nconst names = [&#039;url&#039;, &#039;scheme&#039;, &#039;slash&#039;, &#039;host&#039;, &#039;port&#039;, &#039;path&#039;, &#039;query&#039;, &#039;hash&#039;];\n\nconst longestString = names.reduce((prev, current) =&gt; {\n  return prev.length &lt; current.length ? current : prev;\n}, &#039;&#039;);\n\nconst maxLength = longestString.length;\n\nconsole.log(&quot;maxLength:&quot; + maxLength);  \/\/ maxLength: 6\n\n<\/code><\/pre>\n\n\n\n<script type=\"text\/javascript\">\njQuery(function($){\n$(\".jp-video\").hide();\n});\n<\/script>\n\n\n\n<p><br><a href=\"http:\/\/attacktube.com\/?p=1142\">\u3010JavaScript\u3011Array.prototype.concat\u30e1\u30bd\u30c3\u30c9\u3068Array.prototype.reduce()\u30e1\u30bd\u30c3\u30c9 (attacktube.com)<\/a> <br><a href=\"https:\/\/www.sejuku.net\/blog\/69622\">\u3010JavaScript\u5165\u9580\u3011\u521d\u5fc3\u8005\u3067\u3082\u5206\u304b\u308breduce()\u306e\u4f7f\u3044\u65b9\u3068\u30b5\u30f3\u30d7\u30eb\u4f8b\u307e\u3068\u3081 | \u4f8d\u30a8\u30f3\u30b8\u30cb\u30a2\u30d6\u30ed\u30b0 (sejuku.net)<\/a> ExternalLink<br><a href=\"https:\/\/kde.hateblo.jp\/entry\/2018\/10\/13\/065738\">\u3010JavaScript\u57fa\u790e\u3011Array.prototype.reduce() \u3092\u3057\u3063\u304b\u308a\u7406\u89e3\u3059\u308b\uff06\u30b5\u30f3\u30d7\u30eb\u96c6 &#8211; KDE BLOG (hateblo.jp)<\/a> ExternalLink<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array.prototype.reduce()\u306f\u3084\u3084\u3053\u3057\u3044\u306a\u3002 \u7c21\u5358\u306a\u4f7f\u3044\u65b9reduce\u30e1\u30bd\u30c3\u30c9\u3067\u914d\u5217\u306e\u4e2d\u306e\u6570\u5024\u3092\u3059\u3079\u3066\u8db3\u3057\u3066\u5408\u8a08\u5024\u3092\u6c42\u3081\u308b\u3002reduce\u30e1\u30bd\u30c3\u30c9\u3067\u914d\u5217\u306e\u4e2d\u306e\u6587\u5b57\u5217\u3092\u3059\u3079\u3066\u9023\u7d50\u3057\u305f\u6587\u5b57\u5217\u3092\u4f5c\u308b\u3002\u521d\u671f\u5024\u306b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1129","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/1129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1129"}],"version-history":[{"count":19,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/1129\/revisions"}],"predecessor-version":[{"id":5261,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/1129\/revisions\/5261"}],"wp:attachment":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}