Зміст курсу
JavaScript Data Structures
JavaScript Data Structures
Challenge: Iteration with for...of
Task
You are given an array of objects. Each object represents a friend's information. The task is to create a for...of
loop to iterate through the array and add one more property to each object that should be: online: false
.
- Use a
for...of
loop to iterate through thefriends
array. - Inside the
for...of
loop, use the dot notation to add the property.
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"}` ); }
Expected Output:
- To create a
for...of
loop, use the following syntax:for (const element of array) { ... }
. - Use the dot notation (
.
) to add a property (online
) and assign it the valuefalse
.
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"}` ); }
Дякуємо за ваш відгук!