javascript difference between two arrays
let difference = arr1.filter(x => !arr2.includes(x));
javascript difference between two arrays
let difference = arr1.filter(x => !arr2.includes(x));
javascript get array difference
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"]
Diff Two Arrays
const diffArray = (arr1, arr2) => {
// Store the different elements
const diffArray = []
// Concat both arrays
const uniqueArr = [...new Set([...arr1, ...arr2])];
// OR const uniqueArr = [...new Set(arr1.concat(...arr2))]
// Loop through the unique array and confirm that each element in it
// is in both arrays(arr1,arr2), else push element not found in both
// arrays to the diffArray and return it
uniqueArr.forEach(elem => {
if(!arr1.includes(elem) || !arr2.includes(elem))
diffArray.push(elem)
})
return diffArray
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
// With love @kouqhar
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us