Różnica JavaScript między dwiema tablicami
let difference = arr1.filter(x => !arr2.includes(x));
Fragile Fish
let difference = arr1.filter(x => !arr2.includes(x));
function arrayDiff (a1, a2) {
var a = [], diff = [];
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}
for (var k in a) {
diff.push(k);
}
return diff;
}
//usage:
console.log(arrayDiff(['red', 'white','green'], [ 'red','white', 'blue']));//["green", "blue"]
let difference = arr1
.filter(x => !arr2.includes(x))
.concat(arr2.filter(x => !arr1.includes(x)));