--- tags: 30 天軟體工程師體驗營|2025 --- > 建議觀看的影音章節: > JS - event(事件) > # 🏅 JS 任務 Day21 - forEach [`forEach()`](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) 是屬於 **「陣列」** 的方法,會將陣列內的每個元素一一傳入,並依序執行函式。 `forEach()` 內的函式常見會帶有三個參數,以下為用法示例: ```javascript= 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()` 的辦法** ```javascript= 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 陣列中。 ```javascript= let numberAll = [25, 30, 15, 50, 17, 40]; let numberAbove30 = []; let numberUnder30 = []; numberAll.forEach(function(/* 填入參數 */){ if (/* 填入判斷式 */) { /* 程式碼撰寫處 */ } else { /* 程式碼撰寫處 */ } }); console.log(numberAbove30); console.log(numberUnder30); ``` ## 回報流程 1. 將答案貼在 CodePen 並複製 CodePen 連結貼至「回報區」回報 (也可以將答案直接貼至「回報區」) ![](https://i.imgur.com/vftL5i0.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); --> 回報區 --- | 報數 | Discord 名字 | CodePen/其他回饋 | |:----:|:-------------------:|:------------------------------------------------------------------------- |1|Saika|[Codepen](https://codepen.io/saika4501/pen/PwqoyJP)| |2|dean|[Codepen](https://codepen.io/ch933114/pen/ogXNaEO?editors=0011)| |3|han|[Codepen](https://codepen.io/iamHanCheng/pen/bNdGQVo?editors=0011)| | 4 | ying | [CodePen](https://codepen.io/Lara-Lin/pen/PwqoxoW) | | 5 | B | [CodePen](https://codepen.io/Babel777/pen/vEOYQYL) | |6|kelsonhouse|[Codepen](https://codepen.io/Kelson-House/pen/dPoyQGo?editors=0011)| | 7 | 4chan | [CodePen](https://codepen.io/ijuolaqc-the-looper/pen/KwpKrqx?editors=1011) | |8|JHT|[Codepen](https://codepen.io/juanht/pen/ZYGEVBb?editors=0012)| | 9 | 林明德 | [CodePen](https://codepen.io/lwescocn-the-vuer/pen/WbvNPQw?editors=1111) | | 10 | oyll | [CodePen](https://codepen.io/dizzydog-rgb/pen/KwpKJzN?editors=0011) | | 11 | RUDY | [CodePen](https://codepen.io/Rudy-crw/pen/xbGxeGE?editors=0011) | | 12 | pastor | [CodePen](https://codepen.io/peter_hung/pen/ByNaEwM) | | 13 | 登登登 | [CodePen](https://codepen.io/Duncanin/pen/wBavbwv) | | 14 | 7Lun | [Day21-CodePen](https://codepen.io/mfyvqhsn-the-bold/pen/wBavLeP?editors=0011) | | 15 | nora_zizi | [CodePen](https://codepen.io/Nora-Ch/pen/gbpONKG?editors=1111) | | 16 | Hugh | [CodePen](https://codepen.io/Hugh-Chen/pen/pvJoXZq?editors=1111) | | 17 | Chuang | [CodePen](https://codepen.io/uidoytjq-the-solid/pen/azObgjz?editors=0011) | | 18 | WEIWEI | [CodePen](https://codepen.io/weiwei93/pen/emNYPBe) | | 19 | 力文 | [CodePen](https://codepen.io/liwenchiou/pen/GgJRVMX?editors=0011) | | 20 | WAWATA | [CodePen](https://codepen.io/wa-wa-GNOHC-WA-Wa/pen/zxGOLpG) | |21|蛋殼|[Codepen](https://codepen.io/weybrian/pen/KwpwwpL) |22|Holly|[Codepen](https://codepen.io/LEE-HOLLY/pen/azOzzBB) | 23 | HawkeyeLin |[Codepen](https://codepen.io/long-long/pen/YPXPXVM)| | 24 | jingle0900 |[Codepen](https://codepen.io/EvaLi0472/pen/dPoPOaB)| | 25 | Ariel | [CodePen](https://codepen.io/ariel0510/pen/ByNyZxm?editors=0011) | |26|小趴|[Codepen](https://codepen.io/papa2415/pen/JodoyBy)| |27|Esther|[Codepen](https://codepen.io/biunpujm-the-flexboxer/pen/qEdEveb?editors=1111)| | 28 | wei_0982 | [CodePen](https://codepen.io/nico-lai/pen/KwpwWVY) | | 29 | Leonard | [CodePen](https://codepen.io/hyyfjqra-the-sans/pen/GgJZPMo) | | 30 | mercury2508. | [CodePen](https://codepen.io/Mercury2508/pen/jEPBONY) | | 31 | ninii | [CodePen](https://codepen.io/niiniiii/pen/pvJPZmO?editors=1111) | | 32 | William Hsieh | [CodePen](https://codepen.io/lsaimqxa-the-vuer/pen/QwbgNxj?editors=0012) | | 33 | Jessie_Yu | [Jessie_Yu 的 Codepen](https://codepen.io/bakyfkso-the-looper/pen/dPoVYmw?editors=1112)| | 34 | 叮咚 | [Codepen](https://codepen.io/pinchieh-lin/pen/zxvxVao)| <!-- | num | user | [CodePen]() | -->