JavaScript Array reductionRight()
Příklady
Odečtěte čísla v poli, začněte od konce:
const numbers = [175, 50, 25];
document.getElementById("demo").innerHTML
= numbers.reduceRight(myFunc);
function myFunc(total, num) {
return total - num;
}
Odečtěte čísla zprava doleva a zobrazte součet:
const numbers = [2, 45, 30, 100];
document.getElementById("demo").innerHTML = numbers.reduceRight(getSum);
function getSum(total, num) {
return total - num;
}
Definice a použití
Metoda reduceRight()
provádí redukční funkci pro každý prvek pole.
Metoda reduceRight()
funguje zprava doleva.
Metoda reduceRight()
vrací jedinou hodnotu: akumulovaný výsledek funkce.
Metoda reduceRight()
neprovádí funkci pro prázdné prvky.
Viz také:
Syntax
array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
Parametry
Parameter | Description | ||||||||
function() | Required. A function to be run for each element in the array. |
||||||||
Reducer function parameters:
| |||||||||
initialValue | Optional. A value to be passed to the function as the initial value |
Návratová hodnota
Souhrnný výsledek z posledního volání funkce zpětného volání. |
Podpora prohlížeče
reduceRight()
je funkce ECMAScript5 (ES5).
ES5 (JavaScript 2009) plně podporován ve všech prohlížečích:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |