# 🏅 3/3(三) 每日任務 ###### tags: `JS 直播班 - 2021 春季班` 前情提要 --- #### 在物件內新增、修改、取得屬性的方法有以下兩種: 1. 點記法 (Dot notation) ```js= let obj = {}; obj.key = value; // 新增或修改 console.log(obj.key); // 取得 ``` 2. 括弧記法 (Bracket notation) ```js= let obj = {}; // 當 key 的值帶有小數點或是空格時可以使用 (如 -- some.data),記得要加上引號 (單引號或雙引號皆可) obj['key'] = value; // 新增或修改 console.log(obj['key']); // 取得 ``` 如果屬性一開始就存在,則以上的語法會更新該屬性的值; 反之則會建立一個新的屬性。 問題 --- 請透過 console.log ,嘗試透過前面所介紹的 2 種方法依序輸出以下屬性: ```js= let weather = { Country: "高雄", "PM2.5": 35, isSunny: false, rainingRegion: ['鼓山', '鹽埕', '旗津'], rainFall: { 鼓山: 400, 鹽埕: 300, } } // 取得 Country 的值 console.log(); // 取得 "PM2.5" 的值 console.log(); // 取得 isSunny 的值 console.log(); // 取得 rainingRegion 陣列的第一個值 '鼓山' console.log(); // 在 rainFall 新增一個屬性名為「旗津」、設定值為 200,並輸出答案 <!-- 程式碼撰寫處 --> console.log(); ``` 回報流程 --- 將答案寫在 CodePen 並複製 CodePen 連結貼至 thread 中回報就算完成了喔! 解答請參考下圖(需打開程式碼的部分觀看)  <!-- 解答 // 取得 Country 的值 console.log(weather.Country); //方法一 console.log(weather["Country"]); //方法二 // 取得 "PM2.5" 的值 console.log(weather["PM2.5"]); // 取得 isSunny 的值 console.log(weather.isSunny); //方法一 console.log(weather["isSunny"]); //方法二 // 取得 rainingRegion 陣列的第一個值 '鼓山' console.log(weather.rainingRegion[0]); // 在 rainFall 新增一個屬性名為「旗津」、設定值為 200,並輸出結果 weather.rainFall.旗津 = 200; //方法一 console.log(weather.rainFall.旗津); weather["rainFall"]["旗津"] = 200; //方法二 console.log(weather["rainFall"]["旗津"]); -->
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up