aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/javascript/reference/global_objects/array/flatmap/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/fr/web/javascript/reference/global_objects/array/flatmap/index.html')
-rw-r--r--files/fr/web/javascript/reference/global_objects/array/flatmap/index.html25
1 files changed, 10 insertions, 15 deletions
diff --git a/files/fr/web/javascript/reference/global_objects/array/flatmap/index.html b/files/fr/web/javascript/reference/global_objects/array/flatmap/index.html
index d0049d6c3c..4117829f18 100644
--- a/files/fr/web/javascript/reference/global_objects/array/flatmap/index.html
+++ b/files/fr/web/javascript/reference/global_objects/array/flatmap/index.html
@@ -14,10 +14,6 @@ original_slug: Web/JavaScript/Reference/Objets_globaux/Array/flatMap
<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>
-<p class="hidden">\{{EmbedInteractiveExample("pages/js/array-flatmap.html")}}</p>
-
-<p class="hidden">Le code source de cet exemple interactif est disponible dans un dépôt GitHub. Si vous souhaitez contribuez à ces exemples, n'hésitez pas à cloner <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> et à envoyer une <em>pull request</em> !</p>
-
<h2 id="Syntaxe">Syntaxe</h2>
<pre class="syntaxbox"><var>var new_array = arr</var>.flatMap(function <var>callback(currentValue[, index[, array]]) {
@@ -30,7 +26,6 @@ original_slug: Web/JavaScript/Reference/Objets_globaux/Array/flatMap
<dt><code>callback</code></dt>
<dd>La fonction qui produit un élément du nouveau tableau et qui prend trois arguments :
<dl>
- <dt></dt>
<dt><code>currentValue</code></dt>
<dd>La valeur du tableau qui est traitée.</dd>
<dt><code>index</code>{{optional_inline}}</dt>
@@ -55,13 +50,13 @@ original_slug: Web/JavaScript/Reference/Objets_globaux/Array/flatMap
<h3 id="map_et_flatMap"><code>map()</code> et <code>flatMap()</code></h3>
-<pre class="brush: js">var arr1 = <span class="js source"><span class="function-call js meta"><span class="js meta"><span class="brace js meta square"><span>[</span></span><span class="constant decimal js numeric"><span>1</span></span><span class="comma delimiter js meta object"><span>,</span></span><span> </span><span class="brace js meta square"><span>2, 3, 4];
+<pre class="brush: js">var arr1 = [1, 2, 3, 4];
-arr1.map(</span></span></span></span></span>x =&gt; [x * 2]<span class="js source"><span class="function-call js meta"><span class="js meta"><span class="brace js meta square"><span>);</span></span></span></span></span>
+arr1.map(x =&gt; [x * 2]);
// [[2], [4], [6], [8]]
-arr1.flatMap(x =&gt; [x * 2]<span class="js source"><span class="function-call js meta"><span class="js meta"><span class="brace js meta square"><span>);
-// [2, 4, 6, 8]</span></span></span></span></span>
+arr1.flatMap(x =&gt; [x * 2]);
+// [2, 4, 6, 8]
// seul un niveau est aplati
arr1.flatMap(x =&gt; [[x * 2]]);
@@ -81,18 +76,18 @@ tableau1.flatMap(x =&gt; x.split(" "));
<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>
-<div class="line"><span class="js source"><span class="comment double-slash js line"><span class="comment definition js punctuation"><span>//</span></span><span>=&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span></span></div>
+<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 line-numbers language-js"><code class="language-js"><span class="keyword token">var</span> arr <span class="operator token">=</span> <span class="punctuation token">[</span><span class="number token">1</span><span class="punctuation token">,</span> <span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span><span class="punctuation token">,</span> <span class="number token">4</span><span class="punctuation token">]</span><span class="punctuation token">;</span>
+<pre class="brush: js">var arr = [1, 2, 3, 4];
-arr<span class="punctuation token">.</span><span class="function token">flatMap</span><span class="punctuation token">(</span>x <span class="operator token">=</span><span class="operator token">&gt;</span> <span class="punctuation token">[x, </span>x <span class="operator token">*</span> <span class="number token">2</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
-<span class="comment token">// est équivalent à</span>
-arr<span class="punctuation token">.</span><span class="function token">reduce</span><span class="punctuation token">(</span><span class="punctuation token">(</span>acc<span class="punctuation token">,</span> x<span class="punctuation token">)</span> <span class="operator token">=</span><span class="operator token">&gt;</span> acc<span class="punctuation token">.</span><span class="function token">concat</span><span class="punctuation token">(</span><span class="punctuation token">[x, </span>x <span class="operator token">*</span> <span class="number token">2</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">,</span> <span class="punctuation token">[</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
-<span class="comment token">// [1, 2, 2, 4, 3, 6, 4, 8]</span></code></pre>
+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>