0

I've been having some trouble figuring out how to find the differences between 2 multi dimensional arrays.

These are the arrays:

let arr1 = [["Tag2","TES"],["Tag3","TES"],["Fedex Ground","TES"],["Fedex Air","TES"],["AMER","TES"],["CA BC","TES"],["EMEA","TES"],["Express","TES"],["tag-5","TES"]]

let arr2 = [["Tag2","TES"],["Tag3","TES"],["Fedex Ground","TES"],["Fedex Air","TES"],["AMER","TES"],["CA BC","TES"],["EMEA","TES"],["testingTag","TES"],["Express","TES"],["tag-5","TES"], ["tag-6", "TES"]]

And I would like the result to be a 2 dimensional array of the tags that do not exist in both arrays, so in this case:

resultArr = [["testingTag","TES"],["tag-6","TES"]]

I have been able to successfully retrieve all the tags that match in the array.

With this, I then tried looping through the arr2 with all the matching tags and pushing to an array if the tag in the loop did not match the tag in the arr2[i], and before pushing to array I would check if the tag already existed in the array I was pushing too, if it did then I would remove it from the array. But the logic I have explained here did not work out and all it did was confuse me more.

Code to the logic I just tried explaining:

let existingTags = [], deletedTags = [];

    //Checks which tags are not to be deleted
    for(let i=0; i<newTags.length;i++){
        for(let j=0; j<oldTags.length;j++){
            if(newTags[i][0] == oldTags[j][0]){
                existingTags.push(newTags[i][0])
            }
        }
    }
    
    //Pushes to array the tags to be deleted 
    for(let i=0; i<existingTags.length;i++){
        for(let j=0; j<oldTags.length;j++){
            if(existingTags[i] != oldTags[j][0]){
                deletedTags.push(oldTags[j][0])
            }else{
                for(let k=0; k<deletedTags.length; k++){
                    if(oldTags[j][0] == deletedTags[k]){
                        deletedTags.splice(i,1)
                    }
                }
            }
        }
    }

Would there be a simpler and more efficient way of finding the differences between 2 multidimensional arrays?

Omi in a hellcat
  • 558
  • 1
  • 8
  • 29
  • do you have always a pair of key and value? – Nina Scholz Jul 28 '20 at 17:25
  • Does this answer your question? [Is there a library for a Set data type in Javascript?](https://stackoverflow.com/questions/2342749/is-there-a-library-for-a-set-data-type-in-javascript) – Vishesh Mangla Jul 28 '20 at 17:35
  • Does this answer your question? https://stackoverflow.com/questions/1723168/what-is-the-fastest-or-most-elegant-way-to-compute-a-set-difference-using-javasc – Vishesh Mangla Jul 29 '20 at 16:45

2 Answers2

1

You could take an object for key/value pairs and filter the second array by checking the stored value against the actual value.

let array1 = [["Tag2", "TES"], ["Tag3", "TES"], ["Fedex Ground", "TES"], ["Fedex Air", "TES"], ["AMER", "TES"], ["CA BC", "TES"], ["EMEA", "TES"], ["Express", "TES"], ["tag-5", "TES"]],
    array2 = [["Tag2", "TES"], ["Tag3", "TES"], ["Fedex Ground", "TES"], ["Fedex Air", "TES"], ["AMER", "TES"], ["CA BC", "TES"], ["EMEA", "TES"], ["testingTag", "TES"], ["Express", "TES"], ["tag-5", "TES"], ["tag-6", "TES"]],
    values = Object.fromEntries(array1),
    result = array2.filter(([k, v]) => values[k] !== v);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • FYI, this answer: 1) assumes uniqueness of keys; and 2) it doesn't generally produce the correct answer, i.e. the _symmetric_ difference. Instead, `result` here provides only the elements in array2 that are not in array1. The easy fix is to chain another `.concat(array1.filter(...))` command in the end. – Yaniv Jul 28 '20 at 19:42
1

Assuming there is some character (e.g. '_') that cannot appear in the strings, you may convert your problem to dealing with arrays of strings, for example as follows: (I didn't compact the code, to keep some readability)

set1 = new Set(arr1.map(x => x[0]+'_'+x[1]))
set2 = new Set(arr2.map(x => x[0]+'_'+x[1]))
diff1 = arr1.filter(x => !set2.has(x[0]+'_'+x[1]))
diff2 = arr2.filter(x => !set1.has(x[0]+'_'+x[1]))
res = diff1.concat(diff2)
Yaniv
  • 819
  • 4
  • 12