To compare values it can be as simple as a single map() call, though you'll need to account for differing lengths of the two arrays and customize the return value for unmatched values.
const intersection = a.map((x, i) => b[i].data === x.data ? {...x} : undefined);
const a = [],
b = [];
a[3] = {'data': true};
a[4] = {'data': true};
a[5] = {'data': true};
b[2] = {'data': true};
b[3] = {'data': true};
b[4] = {'data': true};
b[5] = {'data': true};
const intersection = a.map((x, i) => b[i].data === x.data ? {...x} : undefined);
console.log(intersection)
To compare keys you need to access the Object.keys() and compare as needed. Here I am using a some() call to check if any keys don't match.
You'll note that if you add a non-matching key (b[5] = {'data': true, 'id': 1};) this will result in an intersection comparing a to b but not the other direction. (All the keys in a are in b but not all the keys of b are in a);
const a = [],
b = [];
a[3] = {'data': true};
a[4] = {'data': true};
a[5] = {'data': true};
b[2] = {'data': true};
b[3] = {'data': true};
b[4] = {'data': true};
b[5] = {'data': true, 'id': 1};
function intersectByAllKeys(arr1, arr2) {
return arr1.map((x, i) => {
const arr2Keys = arr2[i] ? Object.keys(arr2[i]) : [];
return Object.keys(x).some(k => arr2Keys.indexOf(k) < 0) ? undefined : {...x};
});
}
console.log(intersectByAllKeys(a, b))
console.log(intersectByAllKeys(b, a))
In order to return a true intersection you will need to compare both directions. Since map() and forEach() skip undefined entries it is simplest to use a for() loop here. Also, since the intersection will never be longer than the shorter of the supplied array we will iterate based on the length of the shorter array (here achieved by destructuring the arguments after sorting const [a, b] = [...arguments].sort((a, b) => (a.length - b.length))).
const a = [],
b = [];
a[3] = {'data': true};
a[4] = {'data': true, 'id': 3};
a[5] = {'data': true};
b[2] = {'data': true};
b[3] = {'data': true};
b[4] = {'data': true, 'id': 0};
b[5] = {'data': true, 'id': 1};
function missingKeys(a, b) {
return a.some(k => b.indexOf(k) < 0);
}
function intersectSymetricallyByAllKeys(arr1, arr2) {
const [a, b] = [...arguments].sort((a, b) => (a.length - b.length));
const intersection = [];
for (let i=0; i<a.length; i++){
if (!a[i] || !b[i]) {
intersection[i] = undefined;
} else {
const aKeys = Object.keys(a[i]) ?? [];
const bKeys = Object.keys(b[i]) ?? [];
intersection[i] = missingKeys(aKeys,bKeys) || missingKeys(bKeys,aKeys) ?
undefined : {...a[i]}
}
}
return intersection;
}
console.log(intersectSymetricallyByAllKeys(a, b));
console.log(intersectSymetricallyByAllKeys(b, a));
var intersection = a.filter((x, i) => b[i].data === x.data)