JS 迴圈與效能

JS 常見的 for 迴圈 以下由執行速度的快到慢排序 for-loop-cached 1 2 3 4 const listSize = yourList.length; // 精華 for (let index = 0; index < listSize; index++) { // ... } for-loop 1 2 3 for (let index = 0; index < yourList.length; index++) { // ... } forEach 1 2 3 yourList.forEach((element) => { // ... }); for-of 1 2 3 for (const element of yourList) { // ... } 參考資料 https://dev.

JS 常見的陣列操作

JS 做一個 Todolist 吧 以 todolist 為例,做個有 CRUD 功能的待辦清單 1 2 3 4 5 6 7 // todolist 資料容器 let todoListData = [] // 編號用隨機亂數 function newId () { return ~ (Date.now() + (Math.random()*50)) } 新增一筆資料 1 2 3 4 5 6 let newTodoData = { "id": newId(), "title": "買早餐", "info": "要漢堡跟可樂" } todoListData.push(newTodoData) 刪除所有資料 1 todoListData.length = 0 刪除指定資料 1 2 3 4 5 6 7 8 9 10 // 要被刪除的 id const id = 385746993 // 找 id 存在的 index 位置 const listIndex = todoListData.

影音站 Invidious

用 Invidious 看 Youtube Invidious 有著簡潔的網站頁面,可以透過’播放器偏好設定’頁面來做調整,例如’不顯示留言’、‘預設影片品質’ … 等,在觀看影片時,會少了很多廣告及非必要的資訊,頁面更乾淨。 Invidious 此站台功能比較完整且穩定。 站台列表 內還有很多可以選用。 Android 手機可以在瀏覽器的工具列,選擇’加到主畫面’,就可以建立捷徑快速開啟,不用安裝 APP 也能在關閉螢幕時,背景聽影片、聽音樂囉! 站台列表 https://api.invidious.io/

JS ~~ 取整數

用 ~~ 取得整數值 無條件捨去,取得整數值,效能優於 parseInt 1 console.log(~~ -2.34); // -2 用 ~ 判斷陣列的值是否存在 1 2 3 4 const arr = [1, 2, 3]; if (~arr.indexOf(3)) { console.log("find!"); // find! } 參考資料 https://segmentfault.com/a/1190000003731938 https://shunnien.github.io/2017/06/26/JavaScript-Double-bitwise-not/

JS 今天星期幾

顯示今天是星期幾的兩種寫法 使用 switch 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 const dayNumber = new Date().getDay() let day = '星期日' switch(dayNumber){ case 1: day = '星期一' break case 2: day = '星期二' break case 3: day = '星期三' break case 4: day = '星期四' break case 5: day = '星期五' break case 6: day = '星期六' break } console.

JS 找出陣列中不重複的值

用位元運算子 XOR (^) 特性 1 2 3 4 5 function uniqueNumber(nums) { return nums.reduce((a, b) => a ^ b, 0) } console.log(uniqueNumber([1, 2, 3, 1, 2])) // 3 去除重複的值 1 2 3 const origin = [1, 2, 'a', 3, 1, 'b', 'a'] const result = [...(new Set(origin))] console.log(result) // [1, 2, 'a', 3, 'b'] 參考資料 https://pjchender.dev/javascript/js-operator/ https://guahsu.io/2017/06/JavaScript-Duplicates-Array/
0%