如何获得两个数组之间的差异?

时间:2022-01-27 16:26:58

I have 2 arrays:

我有2个数组:

arr1 = [[11,12],[11,13],[11,14], [12,13]];
arr2 = [[11,13],[11,14]];

Expected result [[11,12],[12,13]].

预期结果[[11,12],[12,13]]。

How can I get the difference between the two arrays? I use lodash _.difference but it gives me a wrong answer.

如何获得两个数组之间的差异?我使用lodash _.difference,但它给了我一个错误的答案。

4 个解决方案

#1


0  

Using just javascript, and only for this and similar examples

仅使用javascript,仅适用于此类似示例

var a1 = [[11,12],[11,13],[11,14], [12,13]];
var a2 = [[11,13],[14,11]];
var a3 = a1.filter(ar1 => !a2.some(ar2 => ar1.every(n1 => ar2.includes(n1))))
console.log(a3); // [[11,12],[12,13]]

There are too many criteria to create a generic solution.

创建通用解决方案的标准太多。

For example is [11,12] the same as [12,11], maybe only references to the same array are the same, (ar1 === ar2) as true. What if there are two identical entries in the same array and one in the other, do you filter one out, or keep it? The number of questions can go on for some time and it gets worse the deeper the nesting and the greater the number of types stored in the array.

例如[11,12]与[12,11]相同,也许只有对同一个数组的引用是相同的,(ar1 === ar2)为真。如果同一个阵列中有两个相同的条目而另一个中有一个相同的条目,你会过滤掉一个,还是保留它?问题的数量可以持续一段时间,嵌套越深,数组中存储的类型数越多,问题就越严重。

var a1 = [[11,12],[11,13],[11,14], [12,13]]
var a2 = [[11,13],[14,11],[12,14]];
a3 = [];
[[a1,a2],[a2,a1]].forEach(a=>{
    a3.push(...a[0].filter(
        ar1 => !a[1].some(
            ar2 => ar1.every(
                n1 => ar2.includes(n1)
            )
        )
    ))
});
console.log("[["+a3.join("], [")+"]]")

#2


4  

You need to use _.differenceWith(), because you need to compare the elements of the sub-arrays. By default, it just tests whether the sub-arrays are the same objects, which they presumably aren't.

您需要使用_.differenceWith(),因为您需要比较子数组的元素。默认情况下,它只是测试子数组是否是相同的对象,它们可能不是。

result = _.differenceWith(arr1, arr2, _.isEqual);

#3


0  

Just a few lines of pure JS is sufficient for this separation (union') job. Besides you should make sure to check both arrays over the other.

只需几行纯JS即可完成这种分离(联合')工作。此外,你应该确保检查两个阵列而不是另一个。

var arr1 = [[11,12],[11,13],[11,14],[12,13]],
    arr2 = [[11,13],[11,14],[12,14]];
     res = arr1.reduceRight((p,c,i,a) => { var fi = p.findIndex(s => c.every(n => s.includes(n)));
                                           return fi !== -1 ? (a.splice(i,1),
                                                               p.splice(fi,1),
                                                               p)
                                                            :  p;
                                         },arr2)
               .concat(arr1);
console.log(res);

#4


0  

You could do it without lodash. The tip woud be to map subarrays into strings to make comparison easy

你可以不用lodash做到这一点。提示应该将子数组映射到字符串中以便于比较

var arr1 = [[11,12],[11,13],[11,14], [12,13]];
var arr2 = [[11,13],[11,14],[12,14]];

var res = arr1.concat(arr2).map(x => x.join(",")).filter((x,i,arr) => arr.indexOf(x) === arr.lastIndexOf(x)).map(x => x.split(","));

console.log(res);

#1


0  

Using just javascript, and only for this and similar examples

仅使用javascript,仅适用于此类似示例

var a1 = [[11,12],[11,13],[11,14], [12,13]];
var a2 = [[11,13],[14,11]];
var a3 = a1.filter(ar1 => !a2.some(ar2 => ar1.every(n1 => ar2.includes(n1))))
console.log(a3); // [[11,12],[12,13]]

There are too many criteria to create a generic solution.

创建通用解决方案的标准太多。

For example is [11,12] the same as [12,11], maybe only references to the same array are the same, (ar1 === ar2) as true. What if there are two identical entries in the same array and one in the other, do you filter one out, or keep it? The number of questions can go on for some time and it gets worse the deeper the nesting and the greater the number of types stored in the array.

例如[11,12]与[12,11]相同,也许只有对同一个数组的引用是相同的,(ar1 === ar2)为真。如果同一个阵列中有两个相同的条目而另一个中有一个相同的条目,你会过滤掉一个,还是保留它?问题的数量可以持续一段时间,嵌套越深,数组中存储的类型数越多,问题就越严重。

var a1 = [[11,12],[11,13],[11,14], [12,13]]
var a2 = [[11,13],[14,11],[12,14]];
a3 = [];
[[a1,a2],[a2,a1]].forEach(a=>{
    a3.push(...a[0].filter(
        ar1 => !a[1].some(
            ar2 => ar1.every(
                n1 => ar2.includes(n1)
            )
        )
    ))
});
console.log("[["+a3.join("], [")+"]]")

#2


4  

You need to use _.differenceWith(), because you need to compare the elements of the sub-arrays. By default, it just tests whether the sub-arrays are the same objects, which they presumably aren't.

您需要使用_.differenceWith(),因为您需要比较子数组的元素。默认情况下,它只是测试子数组是否是相同的对象,它们可能不是。

result = _.differenceWith(arr1, arr2, _.isEqual);

#3


0  

Just a few lines of pure JS is sufficient for this separation (union') job. Besides you should make sure to check both arrays over the other.

只需几行纯JS即可完成这种分离(联合')工作。此外,你应该确保检查两个阵列而不是另一个。

var arr1 = [[11,12],[11,13],[11,14],[12,13]],
    arr2 = [[11,13],[11,14],[12,14]];
     res = arr1.reduceRight((p,c,i,a) => { var fi = p.findIndex(s => c.every(n => s.includes(n)));
                                           return fi !== -1 ? (a.splice(i,1),
                                                               p.splice(fi,1),
                                                               p)
                                                            :  p;
                                         },arr2)
               .concat(arr1);
console.log(res);

#4


0  

You could do it without lodash. The tip woud be to map subarrays into strings to make comparison easy

你可以不用lodash做到这一点。提示应该将子数组映射到字符串中以便于比较

var arr1 = [[11,12],[11,13],[11,14], [12,13]];
var arr2 = [[11,13],[11,14],[12,14]];

var res = arr1.concat(arr2).map(x => x.join(",")).filter((x,i,arr) => arr.indexOf(x) === arr.lastIndexOf(x)).map(x => x.split(","));

console.log(res);