aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/javascript/reference/global_objects/array/reduceright
diff options
context:
space:
mode:
authorjulieng <julien.gattelier@gmail.com>2021-08-03 08:03:23 +0200
committerSphinxKnight <SphinxKnight@users.noreply.github.com>2021-09-03 08:08:25 +0200
commitbf8e099b9c8b3c60d60b3712b4fc97b052c39887 (patch)
treec101746d082c9581c94f5937519c7d0e2f4af8cb /files/fr/web/javascript/reference/global_objects/array/reduceright
parent844f5103992238c0c23203286dad16a466e89c97 (diff)
downloadtranslated-content-bf8e099b9c8b3c60d60b3712b4fc97b052c39887.tar.gz
translated-content-bf8e099b9c8b3c60d60b3712b4fc97b052c39887.tar.bz2
translated-content-bf8e099b9c8b3c60d60b3712b4fc97b052c39887.zip
convert content to md
Diffstat (limited to 'files/fr/web/javascript/reference/global_objects/array/reduceright')
-rw-r--r--files/fr/web/javascript/reference/global_objects/array/reduceright/index.md358
1 files changed, 171 insertions, 187 deletions
diff --git a/files/fr/web/javascript/reference/global_objects/array/reduceright/index.md b/files/fr/web/javascript/reference/global_objects/array/reduceright/index.md
index 63222a2191..dbb7001bf8 100644
--- a/files/fr/web/javascript/reference/global_objects/array/reduceright/index.md
+++ b/files/fr/web/javascript/reference/global_objects/array/reduceright/index.md
@@ -11,199 +11,202 @@ tags:
translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight
original_slug: Web/JavaScript/Reference/Objets_globaux/Array/reduceRight
---
-<div>{{JSRef}}</div>
+{{JSRef}}
-<p>La méthode <code><strong>reduceRight()</strong></code> applique une fonction sur un accumulateur et chaque valeur d'un tableau (de la droite vers la gauche) de sorte à réduire le tableau en une seule valeur.</p>
+La méthode **`reduceRight()`** applique une fonction sur un accumulateur et chaque valeur d'un tableau (de la droite vers la gauche) de sorte à réduire le tableau en une seule valeur.
-<div>{{EmbedInteractiveExample("pages/js/array-reduce-right.html")}}</div>
+{{EmbedInteractiveExample("pages/js/array-reduce-right.html")}}
+Voir également {{jsxref("Array.prototype.reduce()")}} pour une méthode qui réduit de gauche à droite.
+## Syntaxe
-<p>Voir également {{jsxref("Array.prototype.reduce()")}} pour une méthode qui réduit de gauche à droite.</p>
+ arr.reduceRight(callback[, valeurInitiale])
-<h2 id="Syntaxe">Syntaxe</h2>
+### Paramètres
-<pre class="syntaxbox"><var>arr</var>.reduceRight(<var>callback</var>[, valeurInitiale])</pre>
+- `callback`
-<h3 id="Paramètres">Paramètres</h3>
+ - : La fonction à éxécuter sur chaque valeur de la liste. Elle utilise quatres arguments :
-<dl>
- <dt><code>callback</code></dt>
- <dd>La fonction à éxécuter sur chaque valeur de la liste. Elle utilise quatres arguments :
- <dl>
- <dt><code>accumulator</code></dt>
- <dd>La valeur précédemment retournée par le dernier appel de la fonction <code>callback</code>, ou <code>valeurInitial</code> si elle est fournie. (Voir ci-après.)</dd>
- <dt><code>valeurCourante</code></dt>
- <dd>La valeur de l'élément courant dans le tableau.</dd>
- <dt><code>index</code></dt>
- <dd>L'index de l'élément du tableau actuellement manipulé.</dd>
- <dt><code>array</code></dt>
- <dd>Le tableau sur lequel <code>reduceRight()</code> est appelée.</dd>
- </dl>
- </dd>
- <dt><code>valeurInitiale</code> {{optional_inline}}</dt>
- <dd>C'est l'objet à utiliser comme accumulateur/premier argument lors du premier appel de la fonction <code>callback</code>. Si aucune valeur n'est fournie, c'est le dernier élément du tableau qui sera utilisé. Si on appelle <code>reduce()</code> ou <code>reduceRight()</code> sur un tableau vide sans fournir de valeur initiale, on aura une exception {{jsxref("TypeError")}}.</dd>
-</dl>
+ - `accumulator`
+ - : La valeur précédemment retournée par le dernier appel de la fonction `callback`, ou `valeurInitial` si elle est fournie. (Voir ci-après.)
+ - `valeurCourante`
+ - : La valeur de l'élément courant dans le tableau.
+ - `index`
+ - : L'index de l'élément du tableau actuellement manipulé.
+ - `array`
+ - : Le tableau sur lequel `reduceRight()` est appelée.
-<h3 id="Valeur_de_retour">Valeur de retour</h3>
+- `valeurInitiale` {{optional_inline}}
+ - : C'est l'objet à utiliser comme accumulateur/premier argument lors du premier appel de la fonction `callback`. Si aucune valeur n'est fournie, c'est le dernier élément du tableau qui sera utilisé. Si on appelle `reduce()` ou `reduceRight()` sur un tableau vide sans fournir de valeur initiale, on aura une exception {{jsxref("TypeError")}}.
-<p>La valeur obtenue grâce à la fonction de réduction.</p>
+### Valeur de retour
-<h2 id="Description">Description</h2>
+La valeur obtenue grâce à la fonction de réduction.
-<p><code>reduceRight</code> exécute la fonction <code>callback</code> une fois pour chaque élément présent dans le tableau, les éléments vides sont ignorés. La fonction <code>callback</code> utilise quatre arguments : la valeur initiale (ou la valeur retournée par le précédent appel de la fonction<code> callback</code>), la valeur de l'élément courant, l'index de l'élément courant, et le tableau qui est parcouru.</p>
+## Description
-<p>L'usage de <code>reduceRight</code> avec définition d'un <code>callback</code> devrait ressembler à ceci :</p>
+`reduceRight` exécute la fonction `callback` une fois pour chaque élément présent dans le tableau, les éléments vides sont ignorés. La fonction `callback` utilise quatre arguments : la valeur initiale (ou la valeur retournée par le précédent appel de la fonction` callback`), la valeur de l'élément courant, l'index de l'élément courant, et le tableau qui est parcouru.
-<pre class="brush: js">array.reduceRight(function(accumulator, valeurCourante, index, array) {
+L'usage de `reduceRight` avec définition d'un `callback` devrait ressembler à ceci :
+
+```js
+array.reduceRight(function(accumulator, valeurCourante, index, array) {
    // ...
-});</pre>
+});
+```
-<p>La première fois que la fonction de callback est appelée, <code>accumulator</code> et <code>valeurCourante</code> peuvent correspondre à un ou deux éléments. Si <code>valeurInitiale</code> est fournie lors de l'appel à <code>reduceRight</code>, alors <code>accumulator</code> sera égale à <code>valeurInitiale</code> et <code>valeurCourante</code> sera égale à la dernière valeur de la liste. Si <code>valeurInitiale</code> n'est pas fournie, alors <code>accumulator</code> sera égale à la dernière valeur de la liste, et <code>valeurCourante</code> sera alors égale à l'avant-dernière valeur du tableau.</p>
+La première fois que la fonction de callback est appelée, `accumulator` et `valeurCourante` peuvent correspondre à un ou deux éléments. Si `valeurInitiale` est fournie lors de l'appel à `reduceRight`, alors `accumulator` sera égale à `valeurInitiale` et `valeurCourante` sera égale à la dernière valeur de la liste. Si `valeurInitiale` n'est pas fournie, alors `accumulator` sera égale à la dernière valeur de la liste, et `valeurCourante` sera alors égale à l'avant-dernière valeur du tableau.
-<p>Si on utilise la méthode <code>reduceRight</code> de la façon suivante :</p>
+Si on utilise la méthode `reduceRight` de la façon suivante :
-<pre class="brush: js">[0, 1, 2, 3, 4].reduceRight(function(accumulator, valeurCourante, index, array) {
+```js
+[0, 1, 2, 3, 4].reduceRight(function(accumulator, valeurCourante, index, array) {
return accumulator + valeurCourante;
});
-</pre>
+```
-<p>La fonction <code>callback</code> sera appelée quatre fois, avec les arguments et les valeurs de retour de chaque appel suivant :</p>
+La fonction `callback` sera appelée quatre fois, avec les arguments et les valeurs de retour de chaque appel suivant :
<table>
- <thead>
- <tr>
- <th scope="col"><code>callback</code></th>
- <th scope="col"><code>accumulator</code></th>
- <th scope="col"><code>valeurCourante</code></th>
- <th scope="col"><code>index</code></th>
- <th scope="col"><code>array</code></th>
- <th scope="col">Valeur renvoyée</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <th scope="row">premier appel</th>
- <td>4</td>
- <td>3</td>
- <td>3</td>
- <td><code>[0,1,2,3,4]</code></td>
- <td>7</td>
- </tr>
- <tr>
- <th scope="row">second appel</th>
- <td>7</td>
- <td><code>2</code></td>
- <td><code>2</code></td>
- <td><code>[0,1,2,3,4]</code></td>
- <td>9</td>
- </tr>
- <tr>
- <th scope="row">troisième appel</th>
- <td>9</td>
- <td>1</td>
- <td>1</td>
- <td><code>[0,1,2,3,4]</code></td>
- <td>10</td>
- </tr>
- <tr>
- <th scope="row">quatrième appel</th>
- <td>10</td>
- <td>0</td>
- <td>0</td>
- <td><code>[0,1,2,3,4]</code></td>
- <td><code>10</code></td>
- </tr>
- </tbody>
+ <thead>
+ <tr>
+ <th scope="col"><code>callback</code></th>
+ <th scope="col"><code>accumulator</code></th>
+ <th scope="col"><code>valeurCourante</code></th>
+ <th scope="col"><code>index</code></th>
+ <th scope="col"><code>array</code></th>
+ <th scope="col">Valeur renvoyée</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <th scope="row">premier appel</th>
+ <td>4</td>
+ <td>3</td>
+ <td>3</td>
+ <td><code>[0,1,2,3,4]</code></td>
+ <td>7</td>
+ </tr>
+ <tr>
+ <th scope="row">second appel</th>
+ <td>7</td>
+ <td><code>2</code></td>
+ <td><code>2</code></td>
+ <td><code>[0,1,2,3,4]</code></td>
+ <td>9</td>
+ </tr>
+ <tr>
+ <th scope="row">troisième appel</th>
+ <td>9</td>
+ <td>1</td>
+ <td>1</td>
+ <td><code>[0,1,2,3,4]</code></td>
+ <td>10</td>
+ </tr>
+ <tr>
+ <th scope="row">quatrième appel</th>
+ <td>10</td>
+ <td>0</td>
+ <td>0</td>
+ <td><code>[0,1,2,3,4]</code></td>
+ <td><code>10</code></td>
+ </tr>
+ </tbody>
</table>
-<p>La valeur retournée par <code>reduceRight</code> sera alors celle du dernier appel de la fonction <code>callback</code> (<code>10</code>).</p>
+La valeur retournée par `reduceRight` sera alors celle du dernier appel de la fonction `callback` (`10`).
-<p>Si vous fournissez une valeur initiale comme second argument à l'appel de <code>reduceRight</code>, le résultat sera alors le suivant :</p>
+Si vous fournissez une valeur initiale comme second argument à l'appel de `reduceRight`, le résultat sera alors le suivant :
-<pre class="brush: js">[0, 1, 2, 3, 4].reduceRight(function(accumulator, valeurCourante, index, array) {
+```js
+[0, 1, 2, 3, 4].reduceRight(function(accumulator, valeurCourante, index, array) {
return accumulator + valeurCourante;
}, 10);
-</pre>
+```
<table>
- <thead>
- <tr>
- <th scope="col"></th>
- <th scope="col"><code>accumulator</code></th>
- <th scope="col"><code>valeurCourante</code></th>
- <th scope="col"><code>index</code></th>
- <th scope="col"><code>array</code></th>
- <th scope="col">Valeur renvoyée</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <th scope="row">premier appel</th>
- <td><code>10</code></td>
- <td>4</td>
- <td>4</td>
- <td><code>[0,1,2,3,4]</code></td>
- <td><code>14</code></td>
- </tr>
- <tr>
- <th scope="row">second appel</th>
- <td>14</td>
- <td>3</td>
- <td>3</td>
- <td><code>[0,1,2,3,4]</code></td>
- <td><code>17</code></td>
- </tr>
- <tr>
- <th scope="row">troisième appel</th>
- <td>17</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">quatrième appel</th>
- <td>19</td>
- <td>1</td>
- <td>1</td>
- <td><code>[0,1,2,3,4]</code></td>
- <td>20</td>
- </tr>
- <tr>
- <th scope="row">cinquième appel</th>
- <td>20</td>
- <td>0</td>
- <td>0</td>
- <td><code>[0,1,2,3,4]</code></td>
- <td><code>20</code></td>
- </tr>
- </tbody>
+ <thead>
+ <tr>
+ <th scope="col"></th>
+ <th scope="col"><code>accumulator</code></th>
+ <th scope="col"><code>valeurCourante</code></th>
+ <th scope="col"><code>index</code></th>
+ <th scope="col"><code>array</code></th>
+ <th scope="col">Valeur renvoyée</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <th scope="row">premier appel</th>
+ <td><code>10</code></td>
+ <td>4</td>
+ <td>4</td>
+ <td><code>[0,1,2,3,4]</code></td>
+ <td><code>14</code></td>
+ </tr>
+ <tr>
+ <th scope="row">second appel</th>
+ <td>14</td>
+ <td>3</td>
+ <td>3</td>
+ <td><code>[0,1,2,3,4]</code></td>
+ <td><code>17</code></td>
+ </tr>
+ <tr>
+ <th scope="row">troisième appel</th>
+ <td>17</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">quatrième appel</th>
+ <td>19</td>
+ <td>1</td>
+ <td>1</td>
+ <td><code>[0,1,2,3,4]</code></td>
+ <td>20</td>
+ </tr>
+ <tr>
+ <th scope="row">cinquième appel</th>
+ <td>20</td>
+ <td>0</td>
+ <td>0</td>
+ <td><code>[0,1,2,3,4]</code></td>
+ <td><code>20</code></td>
+ </tr>
+ </tbody>
</table>
-<p>La valeur renvoyée par <code>reduceRight</code> sera ici <code>20</code>.</p>
+La valeur renvoyée par `reduceRight` sera ici `20`.
-<h2 id="Exemples">Exemples</h2>
+## Exemples
-<h3 id="Additionner_toutes_les_valeurs_d'une_liste">Additionner toutes les valeurs d'une liste</h3>
+### Additionner toutes les valeurs d'une liste
-<pre class="brush: js">var total = [0, 1, 2, 3].reduceRight(function(a, b) {
+```js
+var total = [0, 1, 2, 3].reduceRight(function(a, b) {
return a + b;
});
// total == 6
-</pre>
+```
-<h3 id="Aplatir_une_liste_de_listes">Aplatir une liste de listes</h3>
+### Aplatir une liste de listes
-<pre class="brush: js">var aplati = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
+```js
+var aplati = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
return a.concat(b);
}, []);
// aplati [4, 5, 2, 3, 0, 1]
-</pre>
+```
-<h3 id="Différence_entre_reduce_et_reduceRight">Différence entre <code>reduce</code> et <code>reduceRight</code></h3>
+### Différence entre `reduce` et `reduceRight`
-<pre class="brush: js">var a = ['1', '2', '3', '4','5']
+```js
+var a = ['1', '2', '3', '4','5']
var gauche = a.reduce(function(prev, cur) {
return prev + cur;
});
@@ -213,67 +216,48 @@ var droite = a.reduceRight(function(prev, cur) {
});
console.log(gauche); // "12345"
-console.log(droite); // "54321"</pre>
+console.log(droite); // "54321"
+```
-<h3 id="Composition_de_fonctions_à_l'aide_de_reduce">Composition de fonctions à l'aide de <code>reduce</code></h3>
+### Composition de fonctions à l'aide de `reduce`
-<p>La composition de fonctions consiste en l'enchaînement de n fonctions l'une après l'autre (où les appels sont généralement exécutés de droite à gauche.</p>
+La composition de fonctions consiste en l'enchaînement de n fonctions l'une après l'autre (où les appels sont généralement exécutés de droite à gauche.
-<pre class="brush: js">/**
+```js
+/**
*
* h(x) = f(g(x))
*
* https://fr.wikipedia.org/wiki/Composition_de_fonctions
*/
-const compose = (...args) =&gt; (value) =&gt; args.reduceRight((acc, fn) =&gt; fn(acc), value)
+const compose = (...args) => (value) => args.reduceRight((acc, fn) => fn(acc), value)
// On incrémente un nombre passé en argument
-const inc = (n) =&gt; n + 1
+const inc = (n) => n + 1
// On double la valeur passée en argument
-const double = (n) =&gt; n * 2
+const double = (n) => n * 2
// On compose double(inc(x))
compose(double, inc)(2) // 6
// On compose inc(double(x))
compose(inc, double)(2) // 5
-</pre>
-
-<h2 id="Spécifications">Spécifications</h2>
-
-<table class="standard-table">
- <tbody>
- <tr>
- <th scope="col">Spécification</th>
- <th scope="col">État</th>
- <th scope="col">Commentaires</th>
- </tr>
- <tr>
- <td>{{SpecName('ES5.1', '#sec-15.4.4.22', 'Array.prototype.reduceRight')}}</td>
- <td>{{Spec2('ES5.1')}}</td>
- <td>Définition initiale. Implémentée avec 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>
+```
+
+## Spécifications
+
+| Spécification | État | Commentaires |
+| ------------------------------------------------------------------------------------------------------------------------ | ---------------------------- | ----------------------------------------------------- |
+| {{SpecName('ES5.1', '#sec-15.4.4.22', 'Array.prototype.reduceRight')}} | {{Spec2('ES5.1')}} | Définition initiale. Implémentée avec JavaScript 1.8. |
+| {{SpecName('ES6', '#sec-array.prototype.reduceright', 'Array.prototype.reduceRight')}} | {{Spec2('ES6')}} | |
+| {{SpecName('ESDraft', '#sec-array.prototype.reduceright', 'Array.prototype.reduceRight')}} | {{Spec2('ESDraft')}} | |
-<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2>
+## Compatibilité des navigateurs
-<p>{{Compat("javascript.builtins.Array.reduceRight")}}</p>
+{{Compat("javascript.builtins.Array.reduceRight")}}
-<h2 id="Voir_aussi">Voir aussi</h2>
+## Voir aussi
-<ul>
- <li>{{jsxref("Array.prototype.reduce()")}}</li>
-</ul>
+- {{jsxref("Array.prototype.reduce()")}}