我想从两个数组中删除重复的数组,但只显示其中一个数组,我该怎么做?我想要结果[1,4]
const arr1 = [1, 2, 3, 4];
const arr2 = [2, 3, 5, 6]
function arrayUniq(arr1, arr2) {
enter code here
}
正如@Titus所说,如果其中一个值重复,只需过滤数组进行比较。
const arr1 = [1, 2, 3, 4];
const arr2 = [2, 3, 5, 6];
function arrayUniq(arr1, arr2) {
const arrays = [...arr1,...arr2]
return arrays.filter(a=> !arr2.includes(a))
}
console.log(arrayUniq(arr1,arr2))
// remove duplicates from arr1 and arr2
function arrayUniq(arr1, arr2) {
let result = [];
// Find unique elements in arr1 & push them into result
arr1.forEach((e) => (arr2.find((f) => f === e) ? null : result.push(e)));
// Find unique elements in arr2 & push them into result
arr2.forEach((e) => (arr1.find((f) => f === e) ? null : result.push(e)));
return result;
}
console.log(arrayUniq(arr1, arr2));