Set 的妙用
作者:xie392地址:https://v.douyin.com/iem4sRs7/更新时间:2024-12-21
案例代码
1/**2* 两个数组的并集、交集、差集3* 不能出现重复选项,得到的结果是一个新数组4*/5const arr1 = [32, 22, 55, 33, 11, 33, 5]6const aee2 = [22, 55, 77, 88, 88, 99, 99]78// 并集9const union = [...new Set([...arr1, ...aee2])]1011// 交集12const intersection = [...new Set(arr1.filter(x => aee2.includes(x)))]1314// 差集15const difference = [...new Set(arr1.filter(x => !aee2.includes(x)))]1617console.log(union)18console.log(intersection)19console.log(difference)
输出结果:
1[232, 22, 55, 33, 11,35, 77, 88, 994]5[ 22, 55 ]6[ 32, 33, 11, 5 ]
目录