代碼快速查 —— ES6

數組去重

let arr = [3, 5, 2, 2, 5, 5];

let unique = [...new Set(arr)];

並、交、差集

let a = new Set([1, 2, 3]);

let b = new Set([4, 3, 2]);

  1. 並集:let unique = new Set([...a, ...b]);
  2. 交集:let intersect = new Set([...a]).filter(x => b.has(x));
  3. 差集:let diffrence = new Set([...a]).filter(x => !b.has(x)); // 這裏是一種左差a-b,還有一種右差b-a,自行修改即可

 

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