Diese Übersetzung ist unvollständig. Bitte helfen Sie, diesen Artikel aus dem Englischen zu übersetzen.
Die reduceRight() Methode wendet eine Funktion gegen einen Akkumulator auf jeden Wert des Arrays (von rechts nach links) auf und reduziert es um einen einzelnen Wert.
var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]
Siehe auch Array.prototype.reduce() für links nach rechts Reduzierung.
Syntax
arr.reduceRight(callback[, initialValue])
Parameter
callback- Funktion, welche auf jeden Wert im Array angewandt werden soll, nimmt vier Argumente entgegen:
previousValue- Der zuvor im letzten Aufruf des Rückrufs zurückgegebene Wert, oder
initialValue, wenn unterstützt. (siehe unten) currentValue- Das derzeitige zu verarbeitende Element in dem Array.
index- Der Index des derzeitigen zu verarbeitenden Elements in dem Array.
array- Das Arrayobjekt, in dem das Element enthalten ist.
initialValue- Optional. Ein Objekt, welches beim ersten Aufruf der
callbackFunktion initial aufgerufen werden soll.
Rückgabewert
Der Wert, der aus der Reduktion resultiert.
Beschreibung
reduceRight Führt die Rückruffunktion einmal für jedes im Array vorhandene Element aus, ohne Löcher im Array, wobei vier Argumente empfangen werden: der Initialwert (initialValue) (oder Wert des vorherigen callback call), der Wert des derzeitigen Elements, der derzeitige Index, und das Array auf welches der Durchlauf stattfindet.
Der Aufruf des reduceRight callback würde ungefähr so aussehen:
array.reduceRight(function(previousValue, currentValue, index, array) {
// ...
});
Wenn die Funktion das erste Mal ausgeführt wird, kann der vorherige Wert (previousValue) und der derzeitige Wert (currentValue) eines von zwei Werten sein. Wenn ein Initialwert (initialValue) der Callback-Funktion zu reduceRight mitgegeben wird, dann wird der previousValue und der initialValue gleich dem initialValue in dem Array. Wenn kein initialValue mitgegeben wird, dann ist der previousValue gleich dem Wert in dem Array und currentValue wird gleich zu dem geich dem zweiten bis letzten Wert.
If the array is empty and no initialValue was provided, TypeError would be thrown. If the array has only one element (regardless of position) and no initialValue was provided, or if initialValue is provided but the array is empty, the solo value would be returned without calling callback.
Some example run-throughs of the function would look like this:
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
});
The callback would be invoked four times, with the arguments and return values in each call being as follows:
callback |
previousValue |
currentValue |
index |
array |
return value |
|---|---|---|---|---|---|
| first call | 4 |
3 |
3 |
[0, 1, 2, 3, 4] |
7 |
| second call | 7 |
2 |
2 |
[0, 1, 2, 3, 4] |
9 |
| third call | 9 |
1 |
1 |
[0, 1, 2, 3, 4] |
10 |
| fourth call | 10 |
0 |
0 |
[0, 1, 2, 3, 4] |
10 |
The value returned by reduceRight would be that of the last callback invocation (10).
And if you were to provide an initialValue, the result would look like this:
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
return previousValue + currentValue;
}, 10);
callback |
previousValue |
currentValue |
index |
array |
return value |
|---|---|---|---|---|---|
| first call | 10 |
4 |
4 |
[0, 1, 2, 3, 4] |
14 |
| second call | 14 |
3 |
3 |
[0, 1, 2, 3, 4] |
17 |
| third call | 17 |
2 |
2 |
[0, 1, 2, 3, 4] |
19 |
| fourth call | 19 |
1 |
1 |
[0, 1, 2, 3, 4] |
20 |
| fifth call | 20 |
0 |
0 |
[0, 1, 2, 3, 4] |
20 |
The value returned by reduceRight this time would be, of course, 20.
Examples
Sum up all values within an array
var sum = [0, 1, 2, 3].reduceRight(function(a, b) {
return a + b;
});
// sum is 6
Flatten an array of arrays
var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]
Difference between reduce and reduceRight
var a = ['1', '2', '3', '4', '5'];
var left = a.reduce(function(prev, cur) { return prev + cur; });
var right = a.reduceRight(function(prev, cur) { return prev + cur; });
console.log(left); // "12345"
console.log(right); // "54321"
Polyfill
reduceRight was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of reduceRight in implementations which do not natively support it.
// 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;
};
}
Spezifikationen
| Spezifikation | Status | Kommentar |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) Die Definition von 'Array.prototype.reduceRight' in dieser Spezifikation. |
Standard | Initial definition. Implemented in JavaScript 1.8. |
| ECMAScript 2015 (6th Edition, ECMA-262) Die Definition von 'Array.prototype.reduceRight' in dieser Spezifikation. |
Standard | |
| ECMAScript 2017 Draft (ECMA-262) Die Definition von 'Array.prototype.reduceRight' in dieser Spezifikation. |
Entwurf |
Browserkompatibilität
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Ja) | 3.0 (1.9) | 9 | 10.5 | 4.0 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Ja) | (Ja) | (Ja) | (Ja) | (Ja) | (Ja) |