{"id":1079,"date":"2023-01-29T22:09:34","date_gmt":"2023-01-29T13:09:34","guid":{"rendered":"http:\/\/attacktube.com\/?p=1079"},"modified":"2023-06-27T21:47:58","modified_gmt":"2023-06-27T12:47:58","slug":"%e3%80%90javascript%e3%80%91%e3%82%b9%e3%83%97%e3%83%ac%e3%83%83%e3%83%89%e6%a7%8b%e6%96%87%e3%81%a8map%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/attacktube.com\/?p=1079","title":{"rendered":"\u3010JavaScript\u3011\u30b9\u30d7\u30ec\u30c3\u30c9\u69cb\u6587\u3068map\u3092\u4f7f\u3063\u3066\u307f\u308b"},"content":{"rendered":"\n<p>\u914d\u5217names\u306b\u683c\u7d0d\u3057\u305f\u6587\u5b57\u5217\u306e\u4e2d\u3067\u6700\u9577\u306e\u6587\u5b57\u5217\u3092\u6c42\u3081\u308b<br>Math.max\u3068map\u30e1\u30bd\u30c3\u30c9\u3068\u30b9\u30d7\u30ec\u30c3\u30c8\u69cb\u6587\u3092\u4f7f\u7528\u3057\u305f\u304c\u3001\u3053\u306e\u3068\u304dmap\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308a\u65b0\u3057\u304f\u751f\u6210\u3055\u308c\u305f\u914d\u5217\u306f\u4f7f\u7528\u3055\u308c\u306a\u3044\u305f\u3081\u3001forEach\u3092\u4f7f\u7528\u3059\u308b\u65b9\u304c\u826f\u3044\u6c17\u304c\u3059\u308b\u3002<br>\u30b9\u30d7\u30ec\u30c3\u30c8\u69cb\u6587\u3068map\u3092\u4f7f\u7528\u3059\u308b\u30d1\u30bf\u30fc\u30f3\u306f\u3042\u307e\u308a\u3088\u304f\u306a\u3044\u30d1\u30bf\u30fc\u30f3\u304b\u3082\u3057\u308c\u306a\u3044\u3002<br>\u3084\u3063\u3071\u308a\u3001\u3053\u306e\u3068\u304d\u7121\u99c4\u306a\u914d\u5217\u3092\u751f\u6210\u3057\u3066\u3044\u308b\u304b\u3089\u30c0\u30e1\u306a\u30b3\u30fc\u30c9\u4f8b\u3060\u306a\u3002<\/p>\n\n\n\n<pre><code>\nconst names = ['url', 'scheme', 'slash', 'host', 'port', 'path', 'query', 'hash'];\n\/\/\u3053\u306e\u5834\u5408\u3001map\u30e1\u30bd\u30c3\u30c9\u3067\u65b0\u3057\u304f\u751f\u6210\u3057\u305f\u914d\u5217\u3092\u4f7f\u7528\u3057\u306a\u3044\nconst stringLengthMax = Math.max(...names.map(v =&gt; v.length));\nlet stringLengthMax2 = 0;\n\n\/\/forEach\u3092\u4f7f\u3046\nnames.forEach(v =&gt; {if(stringLengthMax2 &lt; v.length) stringLengthMax2 = v.length;});\n\nconsole.log(\"stringLengthMax = \" + stringLengthMax);\/\/6\nconsole.log(\"stringLengthMax2 = \" + stringLengthMax2);\/\/6\n\n<\/code><\/pre>\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<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\u201d\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>\u305f\u3060\u3057\u3001reduce\u30e1\u30bd\u30c3\u30c9\u306f\u53cd\u5fa9\u51e6\u7406\u3092\u9032\u3081\u308b\u306b\u3064\u308c\u3066\u30a2\u30ad\u30e5\u30e0\u30ec\u30fc\u30bfprev\u306e\u5024\u304c\u66f4\u65b0\u3055\u308c\u3001<br>\u6700\u7d42\u7684\u306b\u306f\u6700\u5f8c\u306b\u6bd4\u8f03\u3055\u308c\u305f\u6587\u5b57\u5217\u304c\u6b8b\u308b\u3002\u3057\u304b\u3057\u3001\u305d\u308c\u304c\u5fc5\u305a\u3057\u3082\u6700\u9577\u306e\u6587\u5b57\u5217\u3067\u3042\u308b\u3068\u306f\u9650\u3089\u306a\u3044\u3002<br><br>\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<p><a href=\"https:\/\/detail.chiebukuro.yahoo.co.jp\/qa\/question_detail\/q10274710215\">javascript\u3067\u6b21\u306e\u3088\u3046\u306bnames\u914d\u5217\u306b\u683c\u7d0d\u3055\u308c\u305f\u6587\u5b57\u5217\u3067\u4e00\u756a&#8230; &#8211; Yahoo!\u77e5\u6075\u888b<\/a> ExternalLink<\/p>\n\n\n\n<script type=\"text\/javascript\">\njQuery(function($){\n$(\".jp-video\").hide();\n});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>\u914d\u5217names\u306b\u683c\u7d0d\u3057\u305f\u6587\u5b57\u5217\u306e\u4e2d\u3067\u6700\u9577\u306e\u6587\u5b57\u5217\u3092\u6c42\u3081\u308bMath.max\u3068map\u30e1\u30bd\u30c3\u30c9\u3068\u30b9\u30d7\u30ec\u30c3\u30c8\u69cb\u6587\u3092\u4f7f\u7528\u3057\u305f\u304c\u3001\u3053\u306e\u3068\u304dmap\u30e1\u30bd\u30c3\u30c9\u306b\u3088\u308a\u65b0\u3057\u304f\u751f\u6210\u3055\u308c\u305f\u914d\u5217\u306f\u4f7f\u7528\u3055\u308c\u306a\u3044\u305f\u3081\u3001forEach\u3092\u4f7f\u7528\u3059\u308b\u65b9\u304c\u826f\u3044\u6c17 [&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-1079","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\/1079","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=1079"}],"version-history":[{"count":10,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/1079\/revisions"}],"predecessor-version":[{"id":3540,"href":"https:\/\/attacktube.com\/index.php?rest_route=\/wp\/v2\/posts\/1079\/revisions\/3540"}],"wp:attachment":[{"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/attacktube.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}