チャレンジ:for...ofによる効率的な配列の反復処理
メニューを表示するにはスワイプしてください
課題
配列として友人情報のオブジェクトが与えられています。各オブジェクトに新しいプロパティ for...of を追加するために、online: false ループを作成してください。
for...ofループを使ってfriends配列を反復処理します。for...ofループ内でドット記法を用いてプロパティを追加します。
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"}` ); }
期待される出力:
Friend name is Gail Russel, offline
Friend name is Mrs. Laurie Wunsch, offline
for...ofループを作成するには、次の構文を使用:for (const element of array) { ... }- ドット記法(
.)を使ってプロパティ(online)を追加し、その値にfalseを代入。
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"}` ); }
すべて明確でしたか?
フィードバックありがとうございます!
セクション 4. 章 8
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 4. 章 8