Miêu tả

Phương thức array reduceRight() trong Javascript áp dụng một hàm đồng thời với hai giá trị của mảng (từ phải qua trái) khi để giảm nó tới một giá trị đơn.

Cú pháp

Cú pháp của nó như sau:

array.reduceRight(callback[, initialValue]);

Chi tiết về tham số

  • callback − Hàm thực thi trên mỗi giá trị của mảng.

  • initialValue − Đối tượng được sử dụng như là tham số đầu tiên tới lần gọi đầu của callback.

Trả về giá trị

Trả về giá trị đơn bên phải đã giảm của mảng.

Khả năng tương thích

Phương thức này là một phần JavaScript bổ sung tới chuẩn ECMA-262. Để khiến nó làm việc, bạn thêm code sau vào phần trên cùng của script của bạn.

if (!Array.prototype.reduceRight)
{
   Array.prototype.reduceRight = function(fun /*, initial*/)
   {
      var len = this.length;
      if (typeof fun != "function")
      throw new TypeError();
      
      // no value to return if no initial value, empty array
      if (len == 0 && arguments.length == 1)
      throw new TypeError();
      var i = len - 1;
      
      if (arguments.length >= 2)
      {
         var rv = arguments[1];
      }
      else
      {
         do
         {
            if (i in this)
            {
               rv = this[i--];
               break;
            }
            
            // if array contains no values, no initial value to return
            if (--i < 0)
            throw new TypeError();
         }
         while (true);
      }
      
      for (; i >= 0; i--)
      {
         if (i in this)
         rv = fun.call(null, rv, this[i], i, this);
      }
      return rv;
   };
}

Ví dụ

Bạn thử ví dụ sau:

<html>
   <head>
      <title>JavaScript Array reduceRight Method</title>
   </head>
   
   <body>
   
      <script type="text/javascript">
         if (!Array.prototype.reduceRight)
         {
            Array.prototype.reduceRight = function(fun /*, initial*/)
            {
               var len = this.length;
               
               if (typeof fun != "function")
               throw new TypeError();
               
               // no value to return if no initial value, empty array
               if (len == 0 && arguments.length == 1)
               throw new TypeError();
               var i = len - 1;
               
               if (arguments.length >= 2)
               {
                  var rv = arguments[1];
               }
               else
               {
                  do
                  {
                     if (i in this)
                     {
                        rv = this[i--];
                        break;
                     }
                     // if array contains no values, no initial value to return
                     if (--i < 0)
                     throw new TypeError();
                  }
                  while (true);
               }
               for (; i >= 0; i--)
               {
                  if (i in this)
                  rv = fun.call(null, rv, this[i], i, this);
               }
               return rv;
            };
         }
         var total = [0, 1, 2, 3].reduceRight(function(a, b) { return a + b; });
         document.write("total is : " + total ); 
      </script>
      
   </body>
</html>

Kết quả

total is : 6

Các bài học JavaScript khác tại s2sontech:




Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Learning English Everyday