2020年3月18日 星期三

typescript 高階函數

寫了一陣子typescript,碰到以下類似的問題,都習慣跑2層for迴圈解掉,
不過明明有好用的函數可用,這邊就簡單的練習一下,並記錄下來。

依某條件建立新的陣列
let c1 = [1,2,3,4,5,6,7,8,9];
let c2 = c1.filter(c=> c > 5 && c < 7 )
                 .map(c => "no"+c );
console.log(c2);

由一個陣列c1產生一個新的陣列c2
let c1 = [1,2,3,4,5,6,7,8,9];
let c2 = c1.map(c=> "no"+c);
console.log(c2);

用一個陣列c2刪除另一個陣列c1
let c1 = [1,2,3,4,5,6,7,8,9];
let c2 = [5,8];
c1 = c1.filter(c => !c2.some(value=> c === value ) );
console.log(c1);

沒有留言:

張貼留言