aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/javascript/reference/global_objects/array/reduceright
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:41:15 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:41:15 -0500
commit4b1a9203c547c019fc5398082ae19a3f3d4c3efe (patch)
treed4a40e13ceeb9f85479605110a76e7a4d5f3b56b /files/de/web/javascript/reference/global_objects/array/reduceright
parent33058f2b292b3a581333bdfb21b8f671898c5060 (diff)
downloadtranslated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.gz
translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.bz2
translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.zip
initial commit
Diffstat (limited to 'files/de/web/javascript/reference/global_objects/array/reduceright')
-rw-r--r--files/de/web/javascript/reference/global_objects/array/reduceright/index.html334
1 files changed, 334 insertions, 0 deletions
diff --git a/files/de/web/javascript/reference/global_objects/array/reduceright/index.html b/files/de/web/javascript/reference/global_objects/array/reduceright/index.html
new file mode 100644
index 0000000000..dfcb7e3a0b
--- /dev/null
+++ b/files/de/web/javascript/reference/global_objects/array/reduceright/index.html
@@ -0,0 +1,334 @@
+---
+title: Array.prototype.reduceRight()
+slug: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight
+tags:
+ - Array
+ - ECMAScript 5
+ - JavaScript
+ - Method
+ - Prototype
+ - polyfill
+translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight
+---
+<div>{{JSRef}}</div>
+
+<p>Die <code><strong>reduceRight()</strong></code> Methode wendet eine Funktion gegen einen Akkumulator auf jeden Wert des Arrays (von rechts nach links) auf und reduziert es um einen einzelnen Wert.</p>
+
+<div>{{EmbedInteractiveExample("pages/js/array-reduce-right.html")}}</div>
+
+
+
+<p>Siehe auch {{jsxref("Array.prototype.reduce()")}} für links nach rechts Reduzierung.</p>
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="syntaxbox"><var>arr</var>.reduceRight(<var>callback</var>[, <var>initialValue</var>])</pre>
+
+<h3 id="Parameter">Parameter</h3>
+
+<dl>
+ <dt><code>callback</code></dt>
+ <dd>Funktion, welche für jeden Wert im Array aufgerufen wird und vier Argumente hat:
+ <dl>
+ <dt><code>previousValue</code></dt>
+ <dd>Der zuvor im letzten Aufruf des Rückrufs zurückgegebene Wert, oder <code>initialValue</code>, wenn unterstützt. (siehe unten)</dd>
+ <dt><code>currentValue</code></dt>
+ <dd>Das derzeitige zu verarbeitende Element in dem Array.</dd>
+ <dt><code>index</code>{{optional_inline}}</dt>
+ <dd>Der Index des derzeitigen zu verarbeitenden Elements in dem Array.</dd>
+ <dt><code>array</code>{{optional_inline}}</dt>
+ <dd>Das Arrayobjekt, in dem das Element enthalten ist.</dd>
+ </dl>
+ </dd>
+ <dt><code>initialValue</code>{{optional_inline}}</dt>
+ <dd>Objekt, welches beim ersten Aufruf der <code>callback</code> Funktion initial benutzt wird. Wenn kein Initialwert angegeben ist wird das letzte Element aus dem Array benutzt. Der aufruf auf einem leeren Array ohne angabe von <code>initialValue</code> führt zu einem Fehler.</dd>
+</dl>
+
+<h3 id="Rückgabewert">Rückgabewert</h3>
+
+<p>Der Wert, der aus der Reduktion resultiert.</p>
+
+<h2 id="Beschreibung">Beschreibung</h2>
+
+<p><code>reduceRight</code> 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 (<code>initialValue)</code> (oder Wert des vorherigen <code>callback</code> Aufrufes), der Wert des derzeitigen Elements, der derzeitige Index, und das Array auf welches der Durchlauf stattfindet.</p>
+
+<p>Der Aufruf des reduceRight <code>callback</code> würde ungefähr so aussehen:</p>
+
+<pre class="brush: js">array.reduceRight(function(previousValue, currentValue, index, array) {
+ // ...
+});
+</pre>
+
+<p>Wenn die Funktion das erste Mal ausgeführt wird, kann der vorherige Wert  (<code>previousValue)</code>  und der derzeitige Wert (<code>currentValue)</code> eines von zwei Werten sein. Wenn ein Initialwert (<code>initialValue)</code> der Callback-Funktion zu <code>reduceRight</code> mitgegeben wird, dann wird der <code>previousValue</code> und <code>der initialValue</code> gleich dem <code>initialValue</code> in dem Array. Wenn kein <code>initialValue</code>  mitgegeben wird, dann ist der <code>previousValue</code> gleich dem Wert in dem Array und <code>currentValue</code> wird gleich zu dem geich dem zweiten bis letzten Wert.</p>
+
+<p>Wenn das Array leer ist und kein <code>initialValue</code> angegeben ist wird ein {{jsxref("TypeError")}} erzeugt. Wenn das Array nur ein Element hat (die Position ist egal) und kein <code>initialValue</code> angegeben ist oder wenn <code>initialValue</code> angegeben ist und das Array leer ist, wird der einzelne Wert zurückgegeben ohne <code>callback</code> aufzurufen.</p>
+
+<p><span id="result_box" lang="de"><span>Einige Beispieldurchläufe der Funktion würden wie folgt aussehen:</span></span></p>
+
+<pre class="brush: js">[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
+ return previousValue + currentValue;
+});
+</pre>
+
+<p>Die <code>callback</code> Funktion wird vier mal aufgerufen. Die Parameter und der Rückgabewert dieser Funktion sind wie folgt:</p>
+
+<table>
+ <thead>
+ <tr>
+ <th scope="col"><code>callback</code></th>
+ <th scope="col"><code>previousValue</code></th>
+ <th scope="col"><code>currentValue</code></th>
+ <th scope="col"><code>index</code></th>
+ <th scope="col"><code>array</code></th>
+ <th scope="col">Rückgabewert</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <th scope="row">1. Aufruf</th>
+ <td><code>4</code></td>
+ <td><code>3</code></td>
+ <td><code>3</code></td>
+ <td><code>[0, 1, 2, 3, 4]</code></td>
+ <td><code>7</code></td>
+ </tr>
+ <tr>
+ <th scope="row">2. Aufruf</th>
+ <td><code>7</code></td>
+ <td><code>2</code></td>
+ <td><code>2</code></td>
+ <td><code>[0, 1, 2, 3, 4]</code></td>
+ <td><code>9</code></td>
+ </tr>
+ <tr>
+ <th scope="row">3. Aufruf</th>
+ <td><code>9</code></td>
+ <td><code>1</code></td>
+ <td><code>1</code></td>
+ <td><code>[0, 1, 2, 3, 4]</code></td>
+ <td><code>10</code></td>
+ </tr>
+ <tr>
+ <th scope="row">4. Aufruf</th>
+ <td><code>10</code></td>
+ <td><code>0</code></td>
+ <td><code>0</code></td>
+ <td><code>[0, 1, 2, 3, 4]</code></td>
+ <td><code>10</code></td>
+ </tr>
+ </tbody>
+</table>
+
+<p>Der Wert, der von <code>reduceRight</code> zurückgegeben wird, ist der Rückgabewert der letzen <code>callback</code> Funktion (<code>10</code>).</p>
+
+<p>Wenn ein <code>initialValue</code> angegeben wird, sieht das Resultat folgendermaßen aus:</p>
+
+<pre class="brush: js">[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
+ return previousValue + currentValue;
+}, 10);
+</pre>
+
+<table>
+ <thead>
+ <tr>
+ <th scope="col"><code>callback</code></th>
+ <th scope="col"><code>previousValue</code></th>
+ <th scope="col"><code>currentValue</code></th>
+ <th scope="col"><code>index</code></th>
+ <th scope="col"><code>array</code></th>
+ <th scope="col">Rückgabewert</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <th scope="row">1. Aufruf</th>
+ <td><code>10</code></td>
+ <td><code>4</code></td>
+ <td><code>4</code></td>
+ <td><code>[0, 1, 2, 3, 4]</code></td>
+ <td><code>14</code></td>
+ </tr>
+ <tr>
+ <th scope="row">2. Aufruf</th>
+ <td><code>14</code></td>
+ <td><code>3</code></td>
+ <td><code>3</code></td>
+ <td><code>[0, 1, 2, 3, 4]</code></td>
+ <td><code>17</code></td>
+ </tr>
+ <tr>
+ <th scope="row">3. Aufruf</th>
+ <td><code>17</code></td>
+ <td><code>2</code></td>
+ <td><code>2</code></td>
+ <td><code>[0, 1, 2, 3, 4]</code></td>
+ <td><code>19</code></td>
+ </tr>
+ <tr>
+ <th scope="row">4. Aufruf</th>
+ <td><code>19</code></td>
+ <td><code>1</code></td>
+ <td><code>1</code></td>
+ <td><code>[0, 1, 2, 3, 4]</code></td>
+ <td><code>20</code></td>
+ </tr>
+ <tr>
+ <th scope="row">5. Aufruf</th>
+ <td><code>20</code></td>
+ <td><code>0</code></td>
+ <td><code>0</code></td>
+ <td><code>[0, 1, 2, 3, 4]</code></td>
+ <td><code>20</code></td>
+ </tr>
+ </tbody>
+</table>
+
+<p>Der von <code>reduceRight</code> zurückgegebene Wert ist in diesem Fall <code>20</code>.</p>
+
+<h2 id="Beispiele">Beispiele</h2>
+
+<h3 id="Alle_Elemente_in_einem_Array_summieren">Alle Elemente in einem Array summieren</h3>
+
+<pre class="brush: js">var sum = [0, 1, 2, 3].reduceRight(function(a, b) {
+ return a + b;
+});
+// sum is 6
+</pre>
+
+<h3 id="Ein_Array_von_Arrays_in_ein_flaches_Array_schreiben">Ein Array von Arrays in ein flaches Array schreiben</h3>
+
+<pre class="brush: js">var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
+ return a.concat(b);
+}, []);
+// flattened is [4, 5, 2, 3, 0, 1]
+</pre>
+
+<h3 id="Eine_Liste_von_asynchronen_Funktionen_mit_Callbacks_hintereinander_laufen_lassen_so_dass_das_Resultat_der_vorherigen_der_nächsten_übergeben_wird">Eine Liste von asynchronen Funktionen mit Callbacks hintereinander laufen lassen, so dass das Resultat der vorherigen der nächsten übergeben wird</h3>
+
+<pre class="brush: js">const waterfall = (...functions) =&gt; (callback, ...args) =&gt;
+ functions.reduceRight(
+ (composition, fn) =&gt; (...results) =&gt; fn(composition, ...results),
+ callback
+ )(...args);
+
+const randInt = max =&gt; Math.floor(Math.random() * max)
+
+const add5 = (callback, x) =&gt; {
+ setTimeout(callback, randInt(1000), x + 5);
+};
+const mult3 = (callback, x) =&gt; {
+ setTimeout(callback, randInt(1000), x * 3);
+};
+const sub2 = (callback, x) =&gt; {
+ setTimeout(callback, randInt(1000), x - 2);
+};
+const split = (callback, x) =&gt; {
+ setTimeout(callback, randInt(1000), x, x);
+};
+const add = (callback, x, y) =&gt; {
+ setTimeout(callback, randInt(1000), x + y);
+};
+const div4 = (callback, x) =&gt; {
+ setTimeout(callback, randInt(1000), x / 4);
+};
+
+const computation = waterfall(add5, mult3, sub2, split, add, div4);
+computation(console.log, 5) // -&gt; 14
+
+// same as:
+
+const computation2 = (input, callback) =&gt; {
+ const f6 = x=&gt; div4(callback, x);
+ const f5 = (x, y) =&gt; add(f6, x, y);
+ const f4 = x =&gt; split(f5, x);
+ const f3 = x =&gt; sub2(f4, x);
+ const f2 = x =&gt; mult3(f3, x);
+ add5(f2, input);
+}</pre>
+
+<h3 id="Unterschied_zwischen_reduce_und_reduceRight">Unterschied zwischen <code>reduce</code> und <code>reduceRight</code></h3>
+
+<pre class="brush: js">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"</pre>
+
+<h2 id="Polyfill">Polyfill</h2>
+
+<p><code>reduceRight</code> wurde im ECMA-262 Standard in der 5. Edition hinzugefügt. Aus diesem Grund ist es in manchen Implementierungen nicht vorhanden. Dieses Problem kann beheben werden indem folgender Quelltext am Anfang des Skripts eingefügt werden, wodurch <code>reduceRight</code> auch in solchen Implementierungen unterstützt werden kann.</p>
+
+<pre class="brush: js">// 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 &gt;&gt;&gt; 0, k = len - 1, value;
+ if (arguments.length &gt;= 2) {
+ value = arguments[1];
+ } else {
+ while (k &gt;= 0 &amp;&amp; !(k in t)) {
+ k--;
+ }
+ if (k &lt; 0) {
+ throw new TypeError('Reduce of empty array with no initial value');
+ }
+ value = t[k--];
+ }
+ for (; k &gt;= 0; k--) {
+ if (k in t) {
+ value = callback(value, t[k], k, t);
+ }
+ }
+ return value;
+ };
+}
+</pre>
+
+<h2 id="Spezifikationen">Spezifikationen</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Spezifikation</th>
+ <th scope="col">Status</th>
+ <th scope="col">Kommentar</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.4.4.22', 'Array.prototype.reduceRight')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td>Initial definition. Implemented in JavaScript 1.8.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-array.prototype.reduceright', 'Array.prototype.reduceRight')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-array.prototype.reduceright', 'Array.prototype.reduceRight')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
+
+<div>
+
+
+<p>{{Compat("javascript.builtins.Array.reduceRight")}}</p>
+</div>
+
+<h2 id="Siehe_auch">Siehe auch</h2>
+
+<ul>
+ <li>{{jsxref("Array.prototype.reduce()")}}</li>
+</ul>