# 集合是什么

一种 “无序且唯一” 的数据结构

js 中使用 SE6 中的集合 Set

使用场景:去重、判断某元素是否在集合中,求交集

  • 去重
const arr = [1,2,3,1,2]
const arr2 = [...new Set(arr)]
1
2
  • 判断元素是否在集合中
const set = new Set(arr)
const has = set.has(3)
1
2
  • 求交集
const set2 = new Set([2, 3])
const set3 = new Set([...set].filter(item => set.has(item)))
1
2