# 🏅 Day 35 - 多筆物件取值 ### Object.keys(), Object.values() 使用練習 * Object.keys() 可以取得指定物件的所有 key 值,並回傳一個陣列。 * Object.values() 可以取得指定物件的所有 value 值,並回傳一個陣列。 ```javascript= const object1 = { a: "字串", b: 42, c: false }; console.log(Object.keys(object1)); // 結果: ["a", "b", "c"] console.log(Object.values(object1)); // 結果: ["字串", 42, false] ``` 問題 --- 請練習使用 Object.keys(),修改資料格式,並存入名稱為 answer 的物件,格式如下圖: ![](https://i.imgur.com/7pA41y0.png) 範例程式碼: ```javascript= let answer = {}; // 請把修改完的資料存入 answer 物件。 let url = "https://raw.githubusercontent.com/hexschool/js-traninging-week6API/main/data.json"; axios.get(url) .then(function(response) { let total = {}; let data = response.data; data.forEach(function(item, i) { if(total[item.jsGroup] === undefined) { total[item.jsGroup] = 1; } else { total[item.jsGroup] += 1; } }); // 將整理好的 total 資料調整成圖中的格式存入 answer 中 /* 程式碼開始 */ /* 程式碼結束 */ console.log(answer); }); ``` ## 回報流程 將答案寫在 CodePen 並複製 CodePen 連結貼至底下回報就算完成了喔! 解答位置請參考下圖(需打開程式碼的部分觀看) ![](https://i.imgur.com/vftL5i0.png) <!-- 解答: ``` let answer = {}; let url = "https://raw.githubusercontent.com/hexschool/js-traninging-week6API/main/data.json"; axios.get(url) .then(function(response) { let total = {}; let data = response.data; data.forEach(function(item, i) { if(total[item.jsGroup] === undefined) { total[item.jsGroup] = 1; } else { total[item.jsGroup] += 1; } }); let keyName = Object.keys(total); keyName.forEach(function(item) { if(item !== "未分組") { answer[`第 ${item} 組人數`] = total[item]; } else { answer[`${item}人數`] = total[item]; } }); console.log(answer); }); ``` 進一步解說解答程式碼: https://codepen.io/jmimiding4104/pen/rNoGmoY?editors=1111 --> 回報區 --- | # | Discord | CodePen / 答案 | | --- | ----- | ----- | | 01 | voke | [CodePen](https://codepen.io/FiSi-the-lessful/pen/rNXdRmw) | | 02 | Hedwig | [CodePen](https://codepen.io/adirehfz-the-encoder/pen/OJKqwxJ?editors=0012) | | 03 | Sonia | [CodePen](https://codepen.io/YUJOU/pen/RwXdBjm?editors=0011) | | 04 | 毛巾 | [CodePen](https://codepen.io/bqdcjboa-the-solid/pen/YzmgjYg) | | 05 | Tatsu | [CodePen](https://codepen.io/chindesu0207/pen/MWNxBqE) | | 06 | tim | [CodePen](https://codepen.io/jskrtivy-the-animator/pen/OJKqoLX?editors=0011) | | 07 | david1819 | [CodePen](https://codepen.io/David-Lin-the-vuer/pen/LYwaBJM) | | 08 | Dolce_墨 | [CodePen](https://codesandbox.io/embed/fcll4s?view=spilt&module=%2Fsrc%2Findex.mjs&hidenavigation=1&expanddevtools=1&editable=0&fontsize=14) | | 09 | 睿睿 | [CodePen](https://codepen.io/uwmrsusb-the-solid/pen/XWvGPqQ?editors=0011) | | 10 | 泊岸 | [CodePen](https://codepen.io/qoq77416416/pen/poMYOZO?editors=0011) | | 11 | yuyuhunter_0509 | [CodePen](https://codepen.io/KRcube/pen/GRVewBK?editors=0011) | | 12 | .tsz_ying | [CodePen](https://codepen.io/bekgnohe-the-selector/pen/poMYQqw?editors=1011) | | 13 | kun._c8_shake_it | [CodePen](https://codepen.io/barry91205/pen/QWeoPwg?editors=1011) | | 14 | GTR150 | [CodePen](https://codepen.io/Wer-Qwe/pen/jOgRMjp?editors=1111) | | 15 | Mike | [CodePen](https://codepen.io/mike2049/pen/zYgXNON?editors=0011) | | 16 | 嚼勁先生 | [CodePen](https://codepen.io/James520284/pen/yLmrvNG) | | 17 | 是在哈囉 | [CodePen](https://codepen.io/miyxdhem-the-solid/pen/NWQmyoJ?editors=1011) | | 18 | Mos | [CodePen](https://codepen.io/mos25399/pen/BaXEPpe?editors=0012) | | 19 | Ariel | [CodePen](https://codepen.io/ariel0510/pen/ZEgZRgb?editors=1011) | | 20 | Kaya | [CodePen](https://codepen.io/kayaribi/pen/gOVyjZV) | |21|Hailey|[CodePen](https://codepen.io/sxbokfja-the-flexboxer/pen/mdNggzE?editors=1012)| | 22 | Chun | [CodePen](https://codepen.io/Chun_debbie/pen/poMBXyv?editors=0011) | | 23 | 610 | [CodePen](https://codepen.io/YI-LING-LIU/pen/poMBMXZ) | | 24 | yuyeh_1212 | [CodePen](https://codepen.io/yuyeh1212/pen/mdNYVNr) | | 25 | jerry_66983 | [CodePen](https://codepen.io/Barry-C/pen/gOVyBqK?editors=0012) | | 26 | Rogan | [CodePen](https://codepen.io/jmimiding4104/pen/rNoGmoY?editors=1111) | | 27 | joylu0902 | [CodePen](https://codepen.io/joylu0902/pen/wvVZMOp) | | 28 | ycl325.etheddie | [CodePen](https://codepen.io/wdvikfuz-the-selector/pen/wvVLzjG) | | 29 | JIA | [CodePen](https://codepen.io/nzaicwlw-the-styleful/pen/LYwvVjx) | | 30 | 陳小廷 | [CodePen](https://codepen.io/ting1124/pen/eYqwVww) | | 31 | Lotte | [CodePen](https://codepen.io/hmkdoxuj-the-decoder/pen/jOgjKJe?editors=0011) | | 32 | JRz | [CodePen](https://codepen.io/JRZHENG/pen/MWNNydL?editors=1111) | | 33 | ab567ab5 | [CodePen](https://codepen.io/WanTzu-Chiang/pen/MWNNepM?editors=0011) | | 34 | Joannehu | [CodePen](https://codepen.io/nelbabkv-the-flexboxer/pen/qBezyGV?editors=1011) | | 35 | Nocab | [CodePen](https://codepen.io/PeihanWang/pen/VYZZZvN?editors=0012) | | 36 | chris | [CodePen](https://codepen.io/chris-chen-the-selector/pen/bNbbGbY?editors=0011) | | 37 | 蘑菇星星 | [CodePen](https://codepen.io/brrrieon-the-vuer/pen/xbKKqJp?editors=1112) | | 40 | Seris | [CodePen](https://codepen.io/Dale-Chien/pen/dPbbQoo) | | 41 | charlottelee | [CodePen](https://codepen.io/char849/pen/dPbyEVQ) | | 42 | kaka_945 | [CodePen](https://codepen.io/kay945/pen/MYgYgKj) | | 43 | KOMATSU PEI | [CodePen](https://codepen.io/Komatsu2021/pen/MYgYgjb?editors=0012) | | 44 | alec1203 | [CodePen](https://codepen.io/soloayaya/pen/gbYbMRw?editors=0012) | | 45 | Emma | [CodePen](https://codepen.io/Emma0919/pen/mybJMap) | | 46 | jimmy.0706 | [CodePen](https://codepen.io/JimmyMao/pen/OPLVoPm?editors=0011) | | 47 | Chuang | [CodePen](https://codepen.io/uidoytjq-the-solid/pen/azoeRRB?editors=0011) | <!-- 可複製下方格式 | | | [CodePen]() | -->