Prototype-array-reduce

提供:Dev Guides
移動先:案内検索

プロトタイプ-reduce()メソッド

この方法では配列が削減されます。1要素配列は一意の要素に変換され、複数要素配列はそのまま返されます。

構文

array.reduce();

戻り値

  • 1要素の配列の場合、一意の要素になります。
  • 複数要素の配列の場合、そのまま残ります。

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>

      <script>
         function showResult() {
            var arr = [10];
            alert("Reduced Element : "  + arr.reduce());

            var arr = [10, 20, 30, 40];
            alert("Reduced Element : "  + arr.reduce().inspect());
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br/>
      <br/>
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

出力