简体   繁体   English

查找2个多维arrays之间的差异

[英]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.我在弄清楚如何找到 2 个多维 arrays 之间的差异时遇到了一些麻烦。

These are the arrays:这些是 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:我希望结果是 arrays 中都不存在的标签的二维数组,所以在这种情况下:

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.有了这个,然后我尝试使用所有匹配的标签循环遍历 arr2 并在循环中的标签与 arr2[i] 中的标签不匹配时推送到数组,然后在推送到数组之前我会检查标签是否已经存在于我正在推动的阵列中,如果确实存在,那么我会将其从阵列中删除。 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?是否有更简单、更有效的方法来查找 2 个多维 arrays 之间的差异?

You could take an object for key/value pairs and filter the second array by checking the stored value against the actual value.您可以将 object 用于键/值对,并通过检查存储的值与实际值来过滤第二个数组。

 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)假设有一些字符(例如'_' )不能出现在字符串中,您可以将您的问题转换为处理字符串的 arrays ,例如如下:(我没有压缩代码,以保持一些可读性)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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