1、两个数组并集,交集,差集

  const array1 = [33, 22, 22, 55, 33, 11, 33, 5, 7, 4]
  const array2 = [11, 11, 33, 44, 66, 66, 99, 99, 0, 789, 765, 765, 44444]

  //~ 不能有重复项,得到新数据
  // 并集
  const union = [...new Set([...array1, ...array2])]

  console.log('并集')
  console.log(union)
  // 交集
  const cross = [
    ...new Set([...array1.filter((item) => array2.includes(item))]),
  ]
  console.log('交集')
  console.log(cross)
  // 差集
  const diff = [...new Set(union.filter((item) => !cross.includes(item)))]
  console.log('差集')
  console.log(diff)

2、例

3、结果

posted on 2023-11-13 09:35  丶凉雨拾忆  阅读(204)  评论(0编辑  收藏  举报