I'm trying to create a simple array comparer that will tell the differences between two different arrays. I've managed to get this much, and it mostly works. However, my problem is that it compares all specific differences in the array, and because this is meant to compare two of the same types of arrays, but one is just an updated version, there is a problem with the counting. Say we have element 1 and element 2. If element 1 is not equal to element 2, then since we're comparing just an updated version of the array and an older version, all other elements are on that list still. Because of this, if we compare the data from all elements after the difference between element 1 and element 2, then all of our updated values should be x higher depending on how many differences there were before those elements. Because of this, after the first difference, every single value is different, even if it was already on the old version of the array. This accurately describes my problem. The first difference comes at 1 and 2, however, because of this difference, every other value is bumped up: 0|0 1|2 2|3 3|4 4|5 5|6 The actual arrays would kinda be like this: ['Auto','Sniper','Citadel','Tank']['Auto','Gunner','Sniper','Citadel','Tank'] As you can see because of the addition of Gunner, all the other argument values coming after Gunner are moved up. But because of this, ever single value after Gunner is now differnt from its original counterpart too, meaning that in what I originally had, it would log everything afterwards.
async function c() {
const fetchO = await fetch('https://a/data');
const fetchN = await fetch('https://a/data');
const O = await fetchO.json();
const N = await fetchN.json();
for(let count in N || O) {
if(N[count] !== O[count]) {
console.table('Old: ', O[count], 'New: ', N[count]);
}
}
}
c();
I've been trying to use a counting mediocre variable as a control so that this error doesn't occur. Like this.
let countControl=0;
async function c() {
const fetchO = await fetch('https://a/data');
const fetchN = await fetch('https://a/data');
const O = await fetchO.json();
const N = await fetchN.json();
for(let count in N || O) {
if(N[count] !== O[count countControl]) {
console.table('Old: ', O[count countControl], 'New: ', N[count]);
if(O.length>N.length){countControl=countControl-1;}
if(O.length<N.length){countControl=countControl 1;}
}
}
}
c();
The problem with this is that count isn't actually a defined variable, so when I do [count countControl], it comes out as being undefined, but if I turn count into a defined variable I'll have to make an updating function on it, which won't work in this situation. How can I add in a working count monitor? Or is there some different way to do this?
CodePudding user response:
If I follow your question correctly, you want to count the differences between the two arrays. If we can make the assumption that the elements in each array are unique (i.e. there are no duplicates) and that order does not matter, it becomes a very simple problem to solve. This would be the equivalent to the set operation "symmetric difference" which gives the set of elements only in one of two given sets. From there, it's just a matter of counting the elements which make up the symmetric difference. This can be implemented as follows.
const a = ['Auto','Sniper','Citadel','Tank'];
const b = ['Auto','Gunner','Sniper','Citadel','Tank'];
const symmetricDifference = (a, b) => {
const difference = new Set(a);
for (const element of new Set(b)) {
if (difference.has(element)) {
difference.delete(element);
} else {
difference.add(element);
}
}
return Array.from(difference);
};
const difference = symmetricDifference(a, b);
const differenceCount = difference.length;
console.log({ difference, differenceCount });
(This implementation still deals with Arrays instead of Sets outside of symmetricDifference()
. Depending on other constraints and the number of objects involved, it might be significantly better for performance to use Sets outside of symmetricDifference()
too.)
If the assumption of uniqueness does not hold, the general algorithm above should be adaptable to instead deal with counts of each unique element. If, however, the assumption of order not mattering does not hold up, the problem becomes much harder in the general case. Depending on your specific case though, there might be shortcuts to be had.