From 2a76c37d0e733dedf8ed87d1e7e46caf3c44667e Mon Sep 17 00:00:00 2001 From: SphinxKnight Date: Fri, 30 Jul 2021 20:18:17 +0200 Subject: Prepare JS French section for Markdown (#1574) * Rm IDs w/ [^h\d] id=" * Remove class="hidden" * Remove hidden for code blocks * rm summary classes * Use Note consistently * Remove sup * Rm code in pre * Fixes dd/dt/dl * Fix some more dd * Remove inline style and useless/craft from span/font * h2m report fixing - Consistent use of notes * h2m report fixing - Consistent use of warnings * h2m report fixing - reword one callout * h2m report fixing - array cruft spans hell * Cleaning the rest of docs for md conversion --- .../global_objects/array/flatmap/index.html | 25 +++++++++------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'files/fr/web/javascript/reference/global_objects/array/flatmap') 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

La méthode flatMap() 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. flatMap est plus efficace que la combinaison de ces deux opérations, souvent réalisées conjointement.

- - - -

Syntaxe

var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
@@ -30,7 +26,6 @@ original_slug: Web/JavaScript/Reference/Objets_globaux/Array/flatMap
  
callback
La fonction qui produit un élément du nouveau tableau et qui prend trois arguments :
-
currentValue
La valeur du tableau qui est traitée.
index{{optional_inline}}
@@ -55,13 +50,13 @@ original_slug: Web/JavaScript/Reference/Objets_globaux/Array/flatMap

map() et flatMap()

-
var arr1 = [1, 2, 3, 4];
+
var arr1 = [1, 2, 3, 4];
 
-arr1.map(x => [x * 2]);
+arr1.map(x => [x * 2]);
 // [[2], [4], [6], [8]]
 
-arr1.flatMap(x => [x * 2]);
-// [2, 4, 6, 8]
+arr1.flatMap(x => [x * 2]);
+// [2, 4, 6, 8]
 
 // seul un niveau est aplati
 arr1.flatMap(x => [[x * 2]]);
@@ -81,18 +76,18 @@ tableau1.flatMap(x => x.split(" "));
 
 

On notera que la longueur de la liste obtenue avec flatMap est différente de la longueur de la liste originale.

-
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
+
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

Équivalent

reduce() et concat()

-
var arr = [1, 2, 3, 4];
+
var arr = [1, 2, 3, 4];
 
-arr.flatMap(x => [x, x * 2]);
-// est équivalent à
-arr.reduce((acc, x) => acc.concat([x, x * 2]), []);
-// [1, 2, 2, 4, 3, 6, 4, 8]
+arr.flatMap(x => [x, x * 2]); +// est équivalent à +arr.reduce((acc, x) => acc.concat([x, x * 2]), []); +// [1, 2, 2, 4, 3, 6, 4, 8]

Spécifications

-- cgit v1.2.3-54-g00ecf