aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/javascript/reference/global_objects/array/flatmap/index.md
diff options
context:
space:
mode:
authorjulieng <julien.gattelier@gmail.com>2021-08-03 08:03:09 +0200
committerSphinxKnight <SphinxKnight@users.noreply.github.com>2021-09-03 08:08:25 +0200
commit844f5103992238c0c23203286dad16a466e89c97 (patch)
treed537708951bb2b61be8192ffacc05a0ce6804f89 /files/fr/web/javascript/reference/global_objects/array/flatmap/index.md
parenta70fd5b73ecb10bec3906640023e2a1a46e118a2 (diff)
downloadtranslated-content-844f5103992238c0c23203286dad16a466e89c97.tar.gz
translated-content-844f5103992238c0c23203286dad16a466e89c97.tar.bz2
translated-content-844f5103992238c0c23203286dad16a466e89c97.zip
move *.html to *.md
Diffstat (limited to 'files/fr/web/javascript/reference/global_objects/array/flatmap/index.md')
-rw-r--r--files/fr/web/javascript/reference/global_objects/array/flatmap/index.md120
1 files changed, 120 insertions, 0 deletions
diff --git a/files/fr/web/javascript/reference/global_objects/array/flatmap/index.md b/files/fr/web/javascript/reference/global_objects/array/flatmap/index.md
new file mode 100644
index 0000000000..4117829f18
--- /dev/null
+++ b/files/fr/web/javascript/reference/global_objects/array/flatmap/index.md
@@ -0,0 +1,120 @@
+---
+title: Array.prototype.flatMap()
+slug: Web/JavaScript/Reference/Global_Objects/Array/flatMap
+tags:
+ - Array
+ - JavaScript
+ - Méthode
+ - Prototype
+ - Reference
+translation_of: Web/JavaScript/Reference/Global_Objects/Array/flatMap
+original_slug: Web/JavaScript/Reference/Objets_globaux/Array/flatMap
+---
+<div>{{JSRef}}</div>
+
+<p>La méthode <code><strong>flatMap()</strong></code> permet d'appliquer une fonction à chaque élément du tableau puis d'aplatir le résultat en un tableau. Cela correspond à l'enchaînement de {{jsxref("Array.prototype.map()")}} suivi de {{jsxref("Array.prototype.flat()")}} de profondeur 1. <code>flatMap</code> est plus efficace que la combinaison de ces deux opérations, souvent réalisées conjointement.</p>
+
+<h2 id="Syntaxe">Syntaxe</h2>
+
+<pre class="syntaxbox"><var>var new_array = arr</var>.flatMap(function <var>callback(currentValue[, index[, array]]) {
+ // return element for new_array
+}</var>[, <var>thisArg</var>])</pre>
+
+<h3 id="Paramètres">Paramètres</h3>
+
+<dl>
+ <dt><code>callback</code></dt>
+ <dd>La fonction qui produit un élément du nouveau tableau et qui prend trois arguments :
+ <dl>
+ <dt><code>currentValue</code></dt>
+ <dd>La valeur du tableau qui est traitée.</dd>
+ <dt><code>index</code>{{optional_inline}}</dt>
+ <dd>L'indice de l'élément du tableau qui est traitée.</dd>
+ <dt><code>array</code>{{optional_inline}}</dt>
+ <dd>Le tableau sur lequel <code>flatMap</code> a été appelée.</dd>
+ </dl>
+ </dd>
+ <dt><code>thisArg</code>{{optional_inline}}</dt>
+ <dd>La valeur à utiliser comme contexte <code>this</code> lors de l'exécution de <code>callback</code>.</dd>
+</dl>
+
+<h3 id="Valeur_de_retour">Valeur de retour</h3>
+
+<p>Un nouveau tableau composé d'éléments résultants de la fonction de rappel (<em>callback</em>) et aplati d'un niveau de profondeur.</p>
+
+<h2 id="Description">Description</h2>
+
+<p>Pour la fonction de rappel, voir {{jsxref("Array.prototype.map()")}}. La méthode <code>flatMap()</code> est identique à un appel de {{jsxref("Array.prototype.map()")}} suivi d'un appel de {{jsxref("Array.prototype.flat()")}} avec la profondeur 1.</p>
+
+<h2 id="Exemples">Exemples</h2>
+
+<h3 id="map_et_flatMap"><code>map()</code> et <code>flatMap()</code></h3>
+
+<pre class="brush: js">var arr1 = [1, 2, 3, 4];
+
+arr1.map(x =&gt; [x * 2]);
+// [[2], [4], [6], [8]]
+
+arr1.flatMap(x =&gt; [x * 2]);
+// [2, 4, 6, 8]
+
+// seul un niveau est aplati
+arr1.flatMap(x =&gt; [[x * 2]]);
+// [[2], [4], [6], [8]]
+</pre>
+
+<p>On peut utiliser un autre exemple où on génère une liste de mots à partir d'une liste de phrases :</p>
+
+<pre class="brush: js">let tableau1 = ["Coucou comment", "", "ça va ?"];
+
+tableau1.map(x =&gt; x.split(" "));
+// [["Coucou", "comment"], [""], ["ça", "va", "?"]]
+
+tableau1.flatMap(x =&gt; x.split(" "));
+// ["Coucou", "comment", "", "ça", "va", "?"]
+</pre>
+
+<p>On notera que la longueur de la liste obtenue avec <code>flatMap</code> est différente de la longueur de la liste originale.</p>
+
+<pre>//=&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9]</pre>
+
+<h2 id="Équivalent">Équivalent</h2>
+
+<h3 id="reduce_et_concat"><code>reduce()</code> et <code>concat()</code></h3>
+
+<pre class="brush: js">var arr = [1, 2, 3, 4];
+
+arr.flatMap(x =&gt; [x, x * 2]);
+// est équivalent à
+arr.reduce((acc, x) =&gt; acc.concat([x, x * 2]), []);
+// [1, 2, 2, 4, 3, 6, 4, 8]</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><a href="https://www.ecma-international.org/ecma-262/10.0/index.html#sec-array.prototype.flatmap">ECMAScript 2019</a></td>
+ <td>Finalisé</td>
+ <td>Proposition initiale</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2>
+
+<p>{{Compat("javascript.builtins.Array.flatMap")}}</p>
+
+<h2 id="Voir_aussi">Voir aussi</h2>
+
+<ul>
+ <li>{{jsxref("Array.prototype.flat()")}}</li>
+ <li>{{jsxref("Array.prototype.map()")}}</li>
+ <li>{{jsxref("Array.prototype.reduce()")}}</li>
+ <li>{{jsxref("Array.prototype.concat()")}}</li>
+</ul>