简体   繁体   中英

Find the differences between 2 multidimensional arrays

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?

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; }

Assuming there is some character (eg '_' ) 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM