Utmaning: Effektiv Array-Iteration med for...of
Uppgift
Du får en array med objekt. Varje objekt representerar information om en vän. Uppgiften är att skapa en for...of-loop för att iterera genom arrayen och lägga till ytterligare en egenskap till varje objekt: online: false.
- Använd en
for...of-loop för att iterera genom arrayenfriends. - Inuti
for...of-loopen, använd punktnotation för att lägga till egenskapen.
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"}` ); }
Förväntad utdata:
Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
- För att skapa en
for...of-loop, använd följande syntax:for (const element of array) { ... }. - Använd punktnotation (
.) för att lägga till en egenskap (online) och tilldela den värdetfalse.
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 allt tydligt?
Tack för dina kommentarer!
Avsnitt 4. Kapitel 8
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 2.27
Utmaning: Effektiv Array-Iteration med for...of
Svep för att visa menyn
Uppgift
Du får en array med objekt. Varje objekt representerar information om en vän. Uppgiften är att skapa en for...of-loop för att iterera genom arrayen och lägga till ytterligare en egenskap till varje objekt: online: false.
- Använd en
for...of-loop för att iterera genom arrayenfriends. - Inuti
for...of-loopen, använd punktnotation för att lägga till egenskapen.
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"}` ); }
Förväntad utdata:
Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
- För att skapa en
for...of-loop, använd följande syntax:for (const element of array) { ... }. - Använd punktnotation (
.) för att lägga till en egenskap (online) och tilldela den värdetfalse.
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 allt tydligt?
Tack för dina kommentarer!
Avsnitt 4. Kapitel 8