Prototype - reduce() 方法

此方法减少数组:单元素数组将转换为其唯一元素,而多元素数组则原封不动地返回。

语法

array.reduce();

返回值

  • 如果是单元素数组,则转换为其唯一元素。
  • 如果是多元素数组,则保持不变。

示例

<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>单击按钮查看结果。</p>
      <br />
      <br />
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

输出

prototype_array_processing.html