最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the difference between two arrays? - Stack Overflow

programmeradmin3浏览0评论

I have 2 arrays:

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

Expected result [[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.

I have 2 arrays:

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

Expected result [[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.

Share Improve this question edited Jan 1, 2017 at 10:20 dda 6,2132 gold badges27 silver badges35 bronze badges asked Jan 1, 2017 at 10:10 HauzHauz 1511 silver badge6 bronze badges 2
  • There are lot of discussion on array pare here: stackoverflow./questions/7837456/… .. the solution would be a extension to it – Neeraj Krishna Commented Jan 1, 2017 at 10:15
  • They are both named arr1 :| – Wais Kamal Commented Jan 1, 2017 at 10:15
Add a ment  | 

4 Answers 4

Reset to default 4

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

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

Using just javascript, and only for this and similar examples

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.

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("], [")+"]]")

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.

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

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

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

发布评论

评论列表(0)

  1. 暂无评论