Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Udfordring: Effektiv Array-Iteration med for...of | Mestring af JavaScript-Arrays
Javascript Datastrukturer

bookUdfordring: Effektiv Array-Iteration med for...of

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.

  1. Brug en for...of-løkke til at iterere gennem friends-arrayet.
  2. Inden i for...of-løkken skal du bruge dot-notation til at tilføje egenskaben.
123456789101112131415161718192021222324
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"}` ); }
copy

Forventet output:

Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
  1. For at oprette et for...of-loop, anvend følgende syntaks: for (const element of array) { ... }.
  2. Brug punktnotation (.) til at tilføje en egenskab (online) og tildel den værdien false.
123456789101112131415161718192021222324
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"}` ); }
copy

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 8

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

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

bookUdfordring: 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.

  1. Brug en for...of-løkke til at iterere gennem friends-arrayet.
  2. Inden i for...of-løkken skal du bruge dot-notation til at tilføje egenskaben.
123456789101112131415161718192021222324
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"}` ); }
copy

Forventet output:

Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
  1. For at oprette et for...of-loop, anvend følgende syntaks: for (const element of array) { ... }.
  2. Brug punktnotation (.) til at tilføje en egenskab (online) og tildel den værdien false.
123456789101112131415161718192021222324
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"}` ); }
copy

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 8
some-alt