Método JavaScript Array reduceRight() – Part 1

A continuación se muestra el ejemplo del método Array reduceRight()

  • Ejemplo: 

HTML

<!DOCTYPE html>
<html>
      
<head>
    <title>
        JavaScript Array reduceRight() Method
    </title>
</head>
  
<body style="text-align:center;">
      
    <h1 style="color: green;">GeeksforGeeks</h1>
      
      
<p>
        Click here to get the Subtract
        of array elements from the left side
    </p>
  
      
    <button onclick="myGeeks()">
        Click Here!
    </button>
      
    <br><br>
      
    Subtract: <span id="GFG"></span>
      
    <!-- Script to use reduceRight method -->
    <script>
        var arr = [175, 50, 25];
  
        function subofArray(total, num) {
            return total - num;
        }
        function myGeeks(item) {
            document.getElementById("GFG").innerHTML
                    = arr.reduceRight(subofArray);
        }
    </script>
</body>
  
</html>          

Producción: 
 

El método arr.reduceRight() en JavaScript se usa para convertir elementos de la array dada de derecha a izquierda en un solo valor.

Sintaxis: 

array.reduceRight( function(total, currentValue, currentIndex, arr), 
initialValue )

Parámetro: este método acepta cinco parámetros, como se mencionó anteriormente y se describe a continuación: 

  • function(total, currentValue, index, arr): Es el parámetro requerido y se usa para ejecutar cada elemento de la array. Contiene cuatro parámetros que se enumeran a continuación: 
    • total: es un parámetro obligatorio y se usa para especificar el valor inicial o el valor devuelto previamente por la función.
    • currentValue: Es un parámetro obligatorio y se utiliza para especificar el valor del elemento actual.
    • currentIndex: es un parámetro opcional y se usa para especificar el índice de array del elemento actual.
    • arr: es un parámetro opcional y se utiliza para especificar el objeto de array al que pertenece el elemento actual.
  • initialValue: es un parámetro opcional y se utiliza para especificar el valor que se pasará a la función como valor inicial.

Ejemplo 1: este ejemplo utiliza el método reduceRight() para devolver la resta de todos los elementos de la array desde la derecha.  

HTML

<!DOCTYPE html>
<html>
      
<head>
    <title>
        JavaScript Array reduceRight() Method
    </title>
</head>
  
<body style="text-align:center;">
      
    <h1 style="color: green;">GeeksforGeeks</h1>
      
      
<p>
        Click here to get the Subtract
        of array elements from right
    </p>
  
      
    <button onclick="myGeeks()">
        Click Here!
    </button>
      
    <br><br>
      
    Subtract: <span id="GFG"></span>
      
    <!-- Script to use reduceRight method -->
    <script>
        var arr = [10, 20, 30, 40, 50, 60];
  
        function subofArray(total, num) {
            return total - num;
        }
        function myGeeks(item) {
            document.getElementById("GFG").innerHTML
                    = arr.reduceRight(subofArray);
        }
    </script>
</body>
  
</html>                    

Producción: 
 

Ejemplo 2: este ejemplo utiliza el método reduceRight() para devolver la suma redonda de todos los elementos de la array. El código que realiza la suma que no se ve afectada por el método reduceRight(). 

HTML

<!DOCTYPE html>
<html>
      
<head>
    <title>
        JavaScript Array reduceRight() Method
    </title>
</head>
  
<body style="text-align:center;">
      
    <h1 style="color: green;">GeeksforGeeks</h1>
      
      
<p>
        Click here to get the sum
        of array elements
    </p>
  
      
    <button onclick="myGeeks()">
        Click Here!
    </button>
      
    <br><br>
      
    Sum: <span id="GFG"></span>
      
    <!-- Script to use reduceRight method -->
    <script>
        var arr = [1.5, 20.3, 11.1, 40.7];
   
        function sumofArray(sum, num) {
            return sum + Math.round(num);
        }
        function myGeeks(item) {
            document.getElementById("GFG").innerHTML
                    = arr.reduceRight(sumofArray, 0);
        }
    </script>
</body>
  
</html>                    

Producción: 
 

Navegadores compatibles: los navegadores compatibles con el método JavaScript Array reduceRight() se enumeran a continuación: 

  • Google cromo 3
  • Microsoft borde 12
  • MozillaFirefox 3.0
  • Safari 5
  • Ópera 10.5
  • explorador de Internet 9

Publicación traducida automáticamente

Artículo escrito por Kanchan_Ray y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *