Sfida: Iterazione Efficiente degli Array con for...of
Compito
Viene fornito un array di oggetti. Ogni oggetto rappresenta le informazioni di un amico. L'obiettivo è creare un ciclo for...of per iterare sull'array e aggiungere una proprietà a ciascun oggetto che deve essere: online: false.
- Utilizzare un ciclo
for...ofper iterare sull'arrayfriends. - All'interno del ciclo
for...of, utilizzare la notazione a punto per aggiungere la proprietà.
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"}` ); }
Output previsto:
Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
- Per creare un ciclo
for...of, utilizzare la seguente sintassi:for (const element of array) { ... }. - Utilizzare la notazione a punto (
.) per aggiungere una proprietà (online) e assegnarle il valorefalse.
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"}` ); }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
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
Sfida: Iterazione Efficiente degli Array con for...of
Scorri per mostrare il menu
Compito
Viene fornito un array di oggetti. Ogni oggetto rappresenta le informazioni di un amico. L'obiettivo è creare un ciclo for...of per iterare sull'array e aggiungere una proprietà a ciascun oggetto che deve essere: online: false.
- Utilizzare un ciclo
for...ofper iterare sull'arrayfriends. - All'interno del ciclo
for...of, utilizzare la notazione a punto per aggiungere la proprietà.
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"}` ); }
Output previsto:
Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
- Per creare un ciclo
for...of, utilizzare la seguente sintassi:for (const element of array) { ... }. - Utilizzare la notazione a punto (
.) per aggiungere una proprietà (online) e assegnarle il valorefalse.
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"}` ); }
Grazie per i tuoi commenti!