tab.map
tab.map(function([value, index, array]){})
Cette fonction retourne un nouveau tableau dont chaque élément a été modifié.
Ce tableau contient autant de cases que le tableau original.
la valeur retournée par la fonction correspond à la valeur mise dans le nouveau tableau.
value : représente un élément du tableau à chaque tour de boucle
index : représente l'index de l'élément du tableau à chaque tour de boucle (facultatif)
array : représente le tableau lui-même (facultatif)
Exemple : Doublement des valeurs du tableau
See the Pen cours-js-array-map by BCMM-W2D (@bcmm-w2d) on CodePen.
Équivalent avec fonction fléchée
const listeNombres = [5, 12, 7, 9, 3, 17, 8, 2, 15, 14];
const tab = listeNombres.map((nb)=> nb * 2);
Équivalent avec forEach
const listeNombres = [5, 12, 7, 9, 3, 17, 8, 2, 15, 14];
const tab = [];
listeNombres.forEach(function (nombre) {
tab.push(nombre * 2);
});
Équivalent avec for of
const listeNombres = [5, 12, 7, 9, 3, 17, 8, 2, 15, 14];
const tab = [];
for (let nombre of listeNombres) {
tab.push(nombre * 2);
}
Équivalent avec for
const listeNombres = [5, 12, 7, 9, 3, 17, 8, 2, 15, 14];
const tab = [];
for (let i = 0; i < listeNombres.length; i++) {
tab[i] = listeNombres[i] * 2;
}
Exemple : Remplace les valeurs paires par 0 et les valeurs impaires par 1
See the Pen cours-js-array-map-2 by BCMM-W2D (@bcmm-w2d) on CodePen.
Équivalent avec fonction fléchée
const listeNombres = [5, 12, 7, 9, 3, 17, 8, 2, 15, 14];
const tab = listeNombres.map((nombre) => nombre % 2);
Équivalent avec forEach
const listeNombres = [5, 12, 7, 9, 3, 17, 8, 2, 15, 14];
const tab = [];
listeNombres.forEach(function (nombre) {
tab.push(nombre % 2);
});
Équivalent avec for of
const listeNombres = [5, 12, 7, 9, 3, 17, 8, 2, 15, 14];
const tab = [];
for (let nombre of listeNombres) {
tab.push(nombre % 2);
}
Équivalent avec for
const listeNombres = [5, 12, 7, 9, 3, 17, 8, 2, 15, 14];
const tab = [];
for (let i = 0; i < listeNombres.length; i++) {
tab[i] = listeNombres[i] % 2;
}
Exemple : Longueur des mots
See the Pen cours-js-array-map-3 by BCMM-W2D (@bcmm-w2d) on CodePen.
Équivalent avec fonction fléchée
const listeMots = ['bonjour', 'hello', 'matin', 'soir', 'journée', 'maintenant', 'après'];
const tab = listeMots.map((mot) => mot.length);
Équivalent avec forEach
const listeMots = ['bonjour', 'hello', 'matin', 'soir', 'journée', 'maintenant', 'après'];
const tab = [];
listeMots.forEach(function (mot) {
tab.push(mot.length);
});
Équivalent avec for of
const listeMots = ['bonjour', 'hello', 'matin', 'soir', 'journée', 'maintenant', 'après'];
const tab = [];
for (let mot of listeMots) {
tab.push(mot.length);
}
Équivalent avec for
const listeMots = ['bonjour', 'hello', 'matin', 'soir', 'journée', 'maintenant', 'après'];
const tab = [];
for (let i = 0; i < listeMots.length; i++) {
tab[i] = listeMots[i].length;
}
Exemple : Première lettre des mots
See the Pen cours-js-array-map-4 by BCMM-W2D (@bcmm-w2d) on CodePen.
Équivalent avec fonction fléchée
const listeMots = ['bonjour', 'hello', 'matin', 'soir', 'journée', 'maintenant', 'après'];
const tab = listeMots.map((mot) => mot[0]);
Équivalent avec forEach
const listeMots = ['bonjour', 'hello', 'matin', 'soir', 'journée', 'maintenant', 'après'];
const tab = [];
listeMots.forEach(function (mot) {
tab.push(mot[0]);
});
Équivalent avec for of
const listeMots = ['bonjour', 'hello', 'matin', 'soir', 'journée', 'maintenant', 'après'];
const tab = [];
for (let mot of listeMots) {
tab.push(mot[0]);
}
Équivalent avec for
const listeMots = ['bonjour', 'hello', 'matin', 'soir', 'journée', 'maintenant', 'après'];
const tab = [];
for (let i = 0; i < listeMots.length; i++) {
tab[i] = listeMots[i][0];
}
Exemple : Liste des noms d'une liste de tableaux associatifs (objet)
See the Pen cours-js-array-map-5 by BCMM-W2D (@bcmm-w2d) on CodePen.
Équivalent avec fonction fléchée
const listePersonnes = [
{id : 8, nom : 'Aule', prenom : 'Lucie', age : 45},
{id : 9, nom : 'Ball', prenom : 'Annie', age : 24},
{id : 10, nom : 'Bon', prenom : 'Jean', age : 65},
{id : 11, nom : 'Braisile', prenom : 'France', age : 13}
];
const tab = listePersonnes.map((personne) => personne.nom);
Équivalent avec forEach
const listePersonnes = [
{id : 8, nom : 'Aule', prenom : 'Lucie', age : 45},
{id : 9, nom : 'Ball', prenom : 'Annie', age : 24},
{id : 10, nom : 'Bon', prenom : 'Jean', age : 65},
{id : 11, nom : 'Braisile', prenom : 'France', age : 13}
];
const tab = [];
listePersonnes.forEach(function (personne) {
tab.push(personne.nom);
});
Équivalent avec for of
const listePersonnes = [
{id : 8, nom : 'Aule', prenom : 'Lucie', age : 45},
{id : 9, nom : 'Ball', prenom : 'Annie', age : 24},
{id : 10, nom : 'Bon', prenom : 'Jean', age : 65},
{id : 11, nom : 'Braisile', prenom : 'France', age : 13}
];
const tab = [];
for (let personne of listePersonnes) {
tab.push(personne.nom);
}
Équivalent avec for
const listePersonnes = [
{id : 8, nom : 'Aule', prenom : 'Lucie', age : 45},
{id : 9, nom : 'Ball', prenom : 'Annie', age : 24},
{id : 10, nom : 'Bon', prenom : 'Jean', age : 65},
{id : 11, nom : 'Braisile', prenom : 'France', age : 13}
];
const tab = [];
for (let i = 0; i < listePersonnes.length; i++) {
tab[i] = listePersonnes[i].nom;
}