<script>
let arr = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1];
// 1. 数组去重
let result = [...new Set(arr)];
console.log("result: ", result);
// 2. 交集
let arr02 = [4, 5, 6, 4, 5];
let result02 = [...new Set(arr)].filter(item => {
let s02 = new Set(arr02);
if (s02.has(item)) {
return true;
} else {
return false;
}
})
// true表示存在交集
let result02Plus = [...new Set(arr)].filter(item => new Set(arr02).has(item));
console.log("交集: ", result02Plus);
// 3. 并集
let union = [...arr, ...arr02];
console.log(union);
// 4. 差集
let diff = [...new Set(arr)].filter(item => !(new Set(arr02).has(item)))
console.log(diff);
</script>
标签:ES6,arr,Set,...,item,let,数组,操作,new
From: https://www.cnblogs.com/openmind-ink/p/16808370.html