UPDATE: This question is not a duplicate of suggested question, since I want to avoid duplicate calls to compare
, since it is transitive.
I have two array of objects, say:
const A = [
{ id: 1, title: 'alice' },
{ id: 2, title: 'bob' },
{ id: 3, title: 'charlie' },
];
const B = [
{ id: 1, title: 'alice' },
{ id: 3, title: 'charlie' },
{ id: 4, title: 'dasha' },
{ id: 5, title: 'eric' },
];
An id
identifies the object univocally (i.e.: title
and all other props of an object with a specific id
are the same for any object in array A
and B
).
To compare them I currently do:
A.forEach(a => {
B.forEach(b => {
compare(a, b);
})
})
The compare()
function is transitive, so compare(a, b) === compare (b, a)
.
How can I avoid comparing twice the same item?
For example, I want do avoid calling compare(a.1, b.3)
and then compare(a.3, b.1)
(where a.1
is item from array A
with id 1
).