Défi : Itération Avec for...of
Tâche
Vous avez un tableau d'objets. Chaque objet représente les informations d'un ami. La tâche consiste à créer une boucle for...of
pour parcourir le tableau et ajouter une propriété supplémentaire à chaque objet qui devrait être : online: false
.
- Utilisez une boucle
for...of
pour parcourir le tableaufriends
. - À l'intérieur de la boucle
for...of
, utilisez la notation par point pour ajouter la propriété.
const friends = [ { name: "Gail Russel", address: "803 Kozey Rapid", phone: "(317) 833-9935 41777", }, { name: "Mrs. Laurie Wunsch", address: "7361 Austin Road", phone: "(728) 884-9049 4760", }, ]; // Use a `for...of` loop for (const friend of ___) { friend.___ = ___; } // Logging specific properties after modifications for (const friend of friends) { console.log( `Friend name is ${friend.name}, ${friend.online ? "online" : "offline"}` ); }
Résultat Attendu:
Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
- Pour créer une boucle
for...of
, utilisez la syntaxe suivante :for (const element of array) { ... }
. - Utilisez la notation par point (
.
) pour ajouter une propriété (online
) et lui attribuer la valeurfalse
.
const friends = [ { name: "Gail Russel", address: "803 Kozey Rapid", phone: "(317) 833-9935 41777", }, { name: "Mrs. Laurie Wunsch", address: "7361 Austin Road", phone: "(728) 884-9049 4760", }, ]; // Use a `for...of` loop for (const friend of friends) { friend.online = false; } // Logging specific properties after modifications for (const friend of friends) { console.log( `Friend name is ${friend.name}, ${friend.online ? "online" : "offline"}` ); }
Tout était clair ?
Merci pour vos commentaires !
Section 4. Chapitre 8