数组的交集,并集,补集
const arr0ne = [1, 2, 3];
const arrTwo = [2, 3, 4, 5, 6];
//数组的交集
const intersectionFn = (arr1, arr2) => {
const result = arr1.filter(item1 => arr2.some(item2 => item1 === item2));
console.log('交集',result);
return result;
}
intersectionFn(arr0ne,arrTwo);
//数组的补集
const complementFn = (arr1, arr2) => {
const result = arr1.filter(item1 => arr2.every(item2 => item1 !== item2));
console.log('补集',result);
return result;
}
complementFn(arr0ne, arrTwo);
//数组的并集
const unionFn = (arr1, arr2) => {
const result = arr1.filter(item1 => arr2.every(item2 => item1 !== item2)).concat(arr2);
console.log('并集',result);
return result;
}
unionFn(arr0ne,arrTwo) ;