Home > Web Front-end > HTML Tutorial > How to apply a function to two values ​​of an array simultaneously from right to left?

How to apply a function to two values ​​of an array simultaneously from right to left?

WBOY
Release: 2023-09-16 23:25:08
forward
1155 people have browsed it

How to apply a function to two values ​​of an array simultaneously from right to left?

Use the reduceRight() method in JavaScript to simultaneously apply a function to two values ​​of an array in a right-to-left manner to reduce it to a single value.

The following are the parameters:

  • callback − Function executed on each value of the array.
  • initialValue − The object used as the first parameter of the callback

Example

You can try running the following code to understand how to use reduceRight() method in JavaScript:

<html>
   <head>
      <title>JavaScript Array reduceRight Method</title>
   </head>
   <body>
      <script>
         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>
Copy after login

The above is the detailed content of How to apply a function to two values ​​of an array simultaneously from right to left?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template