一些好用的小技巧

1.switch替代多個if else判斷

var num = 25;
switch (true) {
    case num < 0:
        alert("Less than 0.");
        break;
    case num >= 0 && num <= 10:
        alert("Between 0 and 10.");
        break;}
    case num >= 10 && num <= 20:
        alert("Between 10 and 20.");
        break;
    default:
        alert("More than 20.");
}

2.簡單的深複製

const b = JSON.parse(JSON.stringfy(a));

3.用?替代多重&&

const a = res && res.data &&res.data.list;
=>
const a = res?.data?.list;

4.拍平多維數組

var entries = [1, [2, 5], [6, 7], 9];
var flat_entries = [].concat(...entries); 
// [1, 2, 5, 6, 7, 9]

更新中…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章