Utfordring: Effektiv Iterasjon over Arrayer med for...of
Oppgave
Du får et array med objekter. Hvert objekt representerer informasjon om en venn. Oppgaven er å lage en for...of-løkke for å iterere gjennom arrayet og legge til en egenskap til hvert objekt som skal være: online: false.
- Bruk en
for...of-løkke for å iterere gjennomfriends-arrayet. - Inne i
for...of-løkka, bruk punktnotasjon for å legge til 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"}` ); }
Forventet utdata:
Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
- For å opprette en
for...of-løkke, bruk følgende syntaks:for (const element of array) { ... }. - Bruk punktnotasjon (
.) for å legge til en egenskap (online) og tilordne den verdienfalse.
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"}` ); }
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain how the `for...of` loop works in this example?
What does the `online: false` property represent for each friend?
Can you show how to add a different property to each object in the array?
Awesome!
Completion rate improved to 2.27
Utfordring: Effektiv Iterasjon over Arrayer med for...of
Sveip for å vise menyen
Oppgave
Du får et array med objekter. Hvert objekt representerer informasjon om en venn. Oppgaven er å lage en for...of-løkke for å iterere gjennom arrayet og legge til en egenskap til hvert objekt som skal være: online: false.
- Bruk en
for...of-løkke for å iterere gjennomfriends-arrayet. - Inne i
for...of-løkka, bruk punktnotasjon for å legge til 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"}` ); }
Forventet utdata:
Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
- For å opprette en
for...of-løkke, bruk følgende syntaks:for (const element of array) { ... }. - Bruk punktnotasjon (
.) for å legge til en egenskap (online) og tilordne den verdienfalse.
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"}` ); }
Takk for tilbakemeldingene dine!