'Il reduceRight()metodo esegue una funzione su ogni elemento dell'array per produrre (ridurlo a) un singolo valore.
'Le reduceRight()opere da destra a sinistra nella matrice. Vedi anche reduce(). 'Il reduceRight()metodo non riduce la matrice originale. 'Questo esempio trova la somma di tutti i numeri in un array: 'Esempio 'var numbers1 = [45, 4, 9, 16, 25]; 'var sum = numbers1.reduceRight(myFunction); 'function myFunction(total, value, index, array) { ' return total + value; '} https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_reduce_right <!DOCTYPE html> <html> <body> <h2>JavaScript Array.reduceRight()</h2> <p>This example finds the sum of all numbers in an array:</p> <p id="demo"></p> <script> var numbers = [45, 4, 9, 16, 25]; var sum = numbers.reduceRight(myFunction); document.getElementById("demo").innerHTML = "The sum is " + sum; function myFunction(total, value, index, array) { return total + value; } </script> </body> </html> 'Si noti che la funzione accetta 4 argomenti: 'Il totale (il valore iniziale / valore restituito in precedenza) 'Il valore dell'oggetto 'L'indice dell'oggetto 'La matrice stessa 'L'esempio sopra non usa i parametri index e array. Puo' essere riscritto a: 'Esempio 'var numbers1 = [45, 4, 9, 16, 25]; 'var sum = numbers1.reduceRight(myFunction); 'function myFunction(total, value) { ' return total + value; '} https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_reduce_right_2 <!DOCTYPE html> <html> <body> <h2>JavaScript Array.reduceRight()</h2> <p>This example finds the sum of all numbers in an array:</p> <p id="demo"></p> <script> var numbers = [45, 4, 9, 16, 25]; var sum = numbers.reduceRight(myFunction); document.getElementById("demo").innerHTML = "The sum is " + sum; function myFunction(total, value) { return total + value; } </script> </body> </html> |