| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | [Back] [Original] |
Get to know MDN better
Esta pgina foi traduzida do ingls pela comunidade. Saiba mais e junte-se comunidade MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since julho de 2015.
O mtodo reduceRight() aplica uma funo um acumulador e cada valor do array (da direita para esquerda) reduzido para um valor nico.
arr.reduceRight(callback[, initialValue])
callbackFuno para executar em cada valor do array, recebendo quatro argumentos:
previousValueO valor anteriormente retornado na ultima invocao do callback, ou o initialValue, se este for o recebido. (Ver abaixo.)
currentValueO valor atualmente sendo processado no array.
indexO ndice do valor atualmente sendo processado no array.
arrayO array que foi chamado para ser reduzido.
initialValueOpcional. Objeto para ser usado como argumento inicial da primeria chamada do callback.
reduceRight executa a funo callback uma vez para cada elemento presente no array, excluindo buracos no array, recebendo quatro argumentos: o valor inicial (ou o valor da chamada anterior do callback), o valor do elemento atual, o ndice do elemento atual, e o array onde a operao est acontecendo.
A chamada ao callback reduceRight ir parecer com uma chamada assim:
array.reduceRight(function (previousValue, currentValue, index, array) {
// ...
});
A primeira vez que a funo chamada, o previousValue e o currentValue podem ser um de dois valores. Se um initialValue foi recebido na chamada do reduceRight, ento o previousValue sera iqual ao initialValue e o currentValue ser igual ao ultimo valor no array. Se o initialValue no foi recebido, ento o previousValue ser igual ao ultimo valor no array e o currentValue ser igual ao penultimo valor no array.
Se o array vazio e nenhum initialValue foi recebido, TypeError ser lanado. Se o array somente tem um elemento (independentemente da posio dele) e o initialValue no foi recebido, ou se o initialValue foi recebido mas o array vazio, o valor em si ser retornado sem chamar o callback.
Alguns exemplos de execues da funo e como ser parecida a chamada:
[0, 1, 2, 3, 4].reduceRight(
function (previousValue, currentValue, index, array) {
return previousValue + currentValue;
},
);
O callback ser invocado quatro vezes, com os argumentos e valores de retornos em cada chamada ser como o seguinte:
previousValue |
currentValue |
index |
array |
return value | |
|---|---|---|---|---|---|
| Primeira chamada | 4 |
3 |
3 |
[0, 1, 2, 3, 4] |
7 |
| Segunda chamada | 7 |
2 |
2 |
[0, 1, 2, 3, 4] |
9 |
| Terceira chamada | 9 |
1 |
1 |
[0, 1, 2, 3, 4] |
10 |
| Quarta chamada | 10 |
0 |
0 |
[0, 1, 2, 3, 4] |
10 |
O valor retornado pelo reduceRight ser o valor retornado pela ultima chamada ao callback(10).
E se voc tambm passou um initialValue, o resultado ir ser como a seguir:
[0, 1, 2, 3, 4].reduceRight(function (
previousValue,
currentValue,
index,
array,
) {
return previousValue + currentValue;
}, 10);
previousValue |
currentValue |
index |
array |
return value | |
|---|---|---|---|---|---|
| Primeira chamada | 10 |
4 |
4 |
[0, 1, 2, 3, 4] |
14 |
| Segunda chamada | 14 |
3 |
3 |
[0, 1, 2, 3, 4] |
17 |
| Terceira chamada | 17 |
2 |
2 |
[0, 1, 2, 3, 4] |
19 |
| Quarta chamada | 19 |
1 |
1 |
[0, 1, 2, 3, 4] |
20 |
| Quinta chamada | 20 |
0 |
0 |
[0, 1, 2, 3, 4] |
20 |
O valor retornado pelo reduceRight desta vez ser, obviamente, 20.
var total = [0, 1, 2, 3].reduceRight(function (a, b) {
return a + b;
});
// total == 6
var flattened = [
[0, 1],
[2, 3],
[4, 5],
].reduceRight(function (a, b) {
return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]
reduceRight foi adicionado no padro ECMA-262 em sua Quinta edio; sendo assim pode no estar presente em todas as implementaes deste padro. Voc pode contornar isso adicionando o seguinte codigo ao inicio do seu script, adicionando a possibilidade de uso do reduceRight em implementaes que no o suportam nativamente.
// Production steps of ECMA-262, Edition 5, 15.4.4.22
// Reference: http://es5.github.io/#x15.4.4.22
if ("function" !== typeof Array.prototype.reduceRight) {
Array.prototype.reduceRight = function (callback /*, initialValue*/) {
"use strict";
if (null === this || "undefined" === typeof this) {
throw new TypeError("Array.prototype.reduce called on null or undefined");
}
if ("function" !== typeof callback) {
throw new TypeError(callback + " is not a function");
}
var t = Object(this),
len = t.length >>> 0,
k = len - 1,
value;
if (arguments.length >= 2) {
value = arguments[1];
} else {
while (k >= 0 && !(k in t)) {
k--;
}
if (k < 0) {
throw new TypeError("Reduce of empty array with no initial value");
}
value = t[k--];
}
for (; k >= 0; k--) {
if (k in t) {
value = callback(value, t[k], k, t);
}
}
return value;
};
}
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.reduceright |
This page was last modified on 22 de mai. de 2026 by MDN contributors.
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()toReversed()toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |