Udfordring: Effektiv Array-Iteration med for...of
Stryg for at vise menuen
Opgave
Du får et array af objekter. Hvert objekt repræsenterer information om en ven. Opgaven er at oprette en for...of-løkke til at iterere gennem arrayet og tilføje endnu en egenskab til hvert objekt, som skal være: online: false.
- Brug en
for...of-løkke til at iterere gennemfriends-arrayet. - Inden i
for...of-løkken skal du bruge dot-notation til at tilføje egenskaben.
123456789101112131415161718192021222324const 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"}` ); }
Forventet output:
Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
- For at oprette et
for...of-loop, anvend følgende syntaks:for (const element of array) { ... }. - Brug punktnotation (
.) til at tilføje en egenskab (online) og tildel den værdienfalse.
123456789101112131415161718192021222324const 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"}` ); }
Var alt klart?
Tak for dine kommentarer!
Sektion 1. Kapitel 29
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Sektion 1. Kapitel 29