# 🏅 DAY25 每日任務:forEach ###### tags: `JS 直播班 - 2021 秋季班` forEach 簡介 --- forEach() 是屬於 **「陣列」** 的方法,會將陣列內的每個元素一一傳入,並執行給定的函式一次。 forEach() 內的函式常見會帶有三個參數,以下為用法示例: ```js= let data = ["a", "b", "c"]; // 參數 item 代表陣列中目前正在被處理的那個元素 // 參數 index 代表陣列中目前正在被處理的那個元素的索引值 // 參數 array 代表被處理的陣列本身,在此為 data data.forEach(function(item, index, array){ console.log(item, index, array); }) ``` 執行結果: ![](https://i.imgur.com/NuRNgjX.png) ### 這邊需要特別提醒,在 forEach() 函式內用 return 是無效的。除非程式碼有誤,否則並沒有中止 forEach() 的辦法 ```js= let data = ["a", "b", "c"]; // data.forEach(function(item, index, array){ console.log(item, index, array); // 程式碼會忽略這個 return return }) ``` 問題 --- 請根據以下要求撰寫程式碼: > 1. 如果 NumberAll 陣列內的值大於或等於 30,則用 push() 方法將其加入 NumberAbove30 陣列中。 > 2. 如果 NumberAll 陣列內的值小於 30,則用 push() 方法將其加入 NumberUnder30 陣列中。 ```js= let NumberAll = [25, 30, 15, 50, 17, 40]; let NumberAbove30 = []; let NumberUnder30 = []; NumberAll.forEach(function(/* 填入參數 */){ if(/* 填入判斷式 */){ /* 程式碼撰寫處 */ }else{ /* 程式碼撰寫處 */ } }); console.log(NumberAbove30); console.log(NumberUnder30); ``` 回報流程 --- 將答案寫在 CodePen 並複製 CodePen 連結貼至 thread 中回報就算完成了喔! 解答請參考下圖(需打開程式碼的部分觀看) ![](https://i.imgur.com/6UoJVtD.png) <!-- 解答 let NumberAll = [25, 30, 15, 50, 17, 40]; let NumberAbove30 = []; let NumberUnder30 = []; NumberAll.forEach(function(item){ if(item >= 30){ NumberAbove30.push(item); }else{ NumberUnder30.push(item); } }); console.log(NumberAbove30); console.log(NumberUnder30); --> | Slack | CodePen | |:------:|:----------------------------------------------------:| | 蔡明達 | https://codepen.io/bmzpfyxe/pen/NWgKQZB?editors=0111 | |CloThEs | https://codepen.io/bogwdnxx-the-sans/pen/zYdJqeK| |Karen Huang| https://codepen.io/Coding_Snorlax/pen/KKvxzbP?editors=0011 | |Aaron Tu|https://codepen.io/aarontu/pen/porOyqo| | YC | https://codepen.io/YCLu/pen/jOLvqRN | |Iris Huang|https://codepen.io/ythuang/pen/abyaNrJ| | 米米 | https://codepen.io/Jameshsu0407/pen/porOyMJ?editors=0011 | |Arista|https://codepen.io/arista-hsieh/pen/jOLvqgO| |洋蔥|https://codepen.io/oikdkmxq-the-looper/pen/PoKdNMX?editors=0011| |lumei|https://codepen.io/l_umei/pen/jOLvrNO| |kk|https://codepen.io/potatokaka/pen/XWaPKWp| |有廖先生(Rain)|https://codepen.io/billpop741/pen/VwzGaoX | |POPEYE | https://codepen.io/popeye_ux/pen/MWvqejj | |jojoy|https://codepen.io/jojoy/pen/LYjJZbd?editors=0011| | Sam | https://codepen.io/sam-hsu/pen/GRvXqWe | |艾瑞克|https://codepen.io/ericla/pen/NWvLrjP?editors=0010| | 劉維倫 | https://codepen.io/lun0223/pen/YzxOWQR?editors=1012 | | Jun Ting Lin | https://codepen.io/jake1155/pen/dyzqXzq | |Yof| https://codepen.io/yangyof/pen/RwZYRgv?editors=1011 | | Elaine Liu | https://codepen.io/elaine7598/pen/WNEgxEQ?editors=0010 | Tim Lin|https://codepen.io/TimmyLin/pen/KKvxMYm | Cheng Pei-hsuan | https://codepen.io/PaCheng/pen/zYdGKpO | | --------------- | ------------------------------------------------------------------ | | Gill | https://codepen.io/Gill-Chin/pen/NWvLrJr?editors=0011 | | 傅劍軒 | https://codepen.io/seonkuraito/pen/bGrxwbw?editors=0010 | | Kelvin Hsu | https://codepen.io/kelvin001/pen/wvqEWZG?editors=1111 | | Anna | https://codepen.io/kycrleao/pen/VwzGKeo?editors=0011 | | Pei Ying | https://codepen.io/Pei-Ying/pen/JjyaRKz?editors=1111 | | Sylvia-H | https://codepen.io/Cosmosheart/pen/dyzqpvb?editors=0011 | | AKI | https://codepen.io/akichen27/pen/rNzZMyZ | | 群嘉 | https://codepen.io/efzdamnp-the-lessful/pen/mdMGrmV?editors=1111 | | Yunei | https://codepen.io/Yunei/pen/NWvLRav | | 大衛 | https://codepen.io/exnsrpjc/pen/VwzBygm?editors=0011 | | Andy Huang | https://codepen.io/HsuHuaHuang/pen/wvqEoNN | | Peggy Tsai | https://codepen.io/pei-chi-tsai/pen/GRvXrJw?editors=0011 | | 許閔翔 | https://codepen.io/oupbzfxq-the-scripter/pen/bGrxgWG | | Bella Shya | https://codepen.io/BellaXie/pen/GRvXrya?editors=0010 | | 蔣秉彣 | https://codepen.io/the-pierre/pen/YzxOZay?editors=1010 | | 陳暐中 | https://codepen.io/wei-z/pen/PoKdmYJ?editors=0011 | | 張喆 | https://codepen.io/wow767t/pen/WNEgjrK | | Genos | https://codepen.io/pb220416/pen/ExveXZv | | ZY Hsu | https://codepen.io/zihyin/pen/Exvevap?editors=0011 | | Trista | https://codepen.io/trista6140/pen/abyaLJv | | Sandy | https://codepen.io/Sandy_L/pen/dyzqVQV?editors=0011 | | Vicky Kao | https://codepen.io/vicky03035/pen/ZEJMvXR | | 黃士桓 | https://codepen.io/shr-huan-huang/pen/NWvLYwP?editors=0011 | | Peter Chen | https://codepen.io/yuckugjy-the-sasster/pen/zYdJjeX | | Jasper | https://codepen.io/li-jasper/pen/ZEJMdoL | | Jocelyn | https://codepen.io/enjoyful/pen/YzxOmyb?editors=0011 | | danny123 | https://codepen.io/binlandz123/pen/qBXJWGr?editors=0010 | | ZOE WU | https://codepen.io/Zoechiueh/pen/BadqapE?editors=0011 | | Steven Chan | https://codepen.io/Steven1220/pen/oNeabzV?editors=1011 | | tingyu | https://codepen.io/dgltu/pen/porxrxa?editors=1012 | | Lai | https://codepen.io/co_lai/pen/Badqbjq?editors=0011 | | 鄭安志 | https://codepen.io/lwmtsgek/pen/eYEPXBp?editors=0011 | | 吉兒 | https://codepen.io/Jillkate/pen/KKvGYdg | | 陳sam | https://codepen.io/euldpliv/pen/yLoRrvz | | 雪莉 | https://codepen.io/utshang216/pen/VwzEOpa | | Joey | https://codepen.io/Joey-Chen/pen/wvqYbYj | | YuriT | https://codepen.io/wenfisht/pen/wvqYZGa | | Yashien Lin | https://codepen.io/YashienLin/pen/QWMVqmo?editors=0010 | | kailun | https://codepen.io/Kailun/pen/vYJQEbY | | lingxuan | https://codepen.io/penglingxuan/pen/KKvrzqe?editors=0010 | | Calon | https://codepen.io/Calon0118/pen/ZEJmWpq | | Bonnie | https://codepen.io/bonnieli1414/pen/yLoQVBK?editors=0011 | | 三隻小貓 | https://codepen.io/bagelover/pen/xxLQWjq?editors=0012 | | Louis | https://codepen.io/Louis164156/pen/PoKxeKY?editors=1111 | | Riley | https://codepen.io/jjpxbprd/pen/vYJQaYZ | | Alvin | https://codepen.io/Alvin20201116/pen/PoKxBdx?editors=1011 | | Jie DU | https://codepen.io/qgqonost-the-flexboxer/pen/gOxQdYq?editors=0011 | | 肉魚 | https://codepen.io/zoechen13/pen/oNeQREg | | 乃萱 | https://codepen.io/nainaikuo/pen/abyPXPr?editors=1011 | | LTL | https://codepen.io/ltlin93/pen/RwZEmqa | | Chia Chi | https://codepen.io/ClaraChen/pen/wvqNGRd?editors=1111 | | Gui | https://codepen.io/guitimliu/pen/gOxqReX | | 吐司 | https://codepen.io/malolulu/pen/oNemKaq | | Ching | https://codepen.io/Jaimedede/pen/qBXvwom?editors=1010 | | debby yeh | https://codepen.io/bhliyoup/pen/oNeOLRb?editors=0011 | | kevinhes | https://codepen.io/kevinhes/pen/YzxMvGj?editors=0012 | | 傑瑞 | https://codepen.io/auatwood909815/pen/VwzJYVP?editors=0011 | | paul | https://codepen.io/printfootya/pen/KKvjgyB?editors=0011 | | 小K | https://codepen.io/kelen1995/pen/YzxoxoV | | Juno Li | https://codepen.io/Juno026/pen/ZEJgGoG?editors=0011 | | Zooey Cheng | https://codepen.io/noraneko0430/pen/gOxVGyd?editors=0011 | | Hamaji | https://codepen.io/hamajibashi/pen/PoJYmva | | Aya | https://codepen.io/NoNameNote/pen/NWaWMjx | | YOYOO | https://codepen.io/lumedkle/pen/ExwaLww?editors=1111 | | Jeff | https://codepen.io/jeff-cheng/pen/GRMpeON | | ShinyChen | https://codepen.io/shinychen/pen/bGoMwGX | | Holliday | https://codepen.io/ichen2006310/pen/mdLpdog |