JS
<script>
let arr=[1,2,3,4,5,4,3,2,1];
let result0=[...new Set(arr)];
console.log(`数组去重${result0}`);
let arr2=[4,5,6,5,6];
let result=[...new Set(arr)].filter(item=>{
let s2=new Set(arr2);
if(s2.has(item)){
return true;
}else{
return false;
}
})
console.log(`交集${result}`);
let result2=[...new Set(arr)].filter(item=>new Set(arr2).has(item));
console.log(`交集${result2}`);
let union=[...new Set([...arr,...arr2])];
console.log(`并集${union}`);
let diff=[...new Set(arr)].filter(item=> !(new Set(arr2).has(item)));
console.log(`并集${diff}`);
</script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30