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

javascript - Given two Arrays, return an Array consisting only of the non-intersecting items - Stack Overflow

programmeradmin0浏览0评论

I'm getting stacked in an issue in JavaScript.

I have two arrays and I want to check if they intersect on some elements then delete those elements and return new array without the intersected elements.

example :

Array A ( 
[0] => 0 [1] => 1 
)

Array B ( 
[0] => 2 [1] => 1 
)

I want to check them and return:

 Array result ( 
[0] => 0 [1] => 2 
)

How can i do this in JavaScript?

I'm getting stacked in an issue in JavaScript.

I have two arrays and I want to check if they intersect on some elements then delete those elements and return new array without the intersected elements.

example :

Array A ( 
[0] => 0 [1] => 1 
)

Array B ( 
[0] => 2 [1] => 1 
)

I want to check them and return:

 Array result ( 
[0] => 0 [1] => 2 
)

How can i do this in JavaScript?

Share Improve this question edited Sep 13, 2011 at 15:34 user113716 323k64 gold badges453 silver badges441 bronze badges asked Sep 13, 2011 at 14:55 sken boysken boy 1472 silver badges11 bronze badges 2
  • 3 stackoverflow.com/questions/1885557/… – Gerry Commented Sep 13, 2011 at 14:58
  • thx for the response but this question is about returning an array with intersected elements . i want to delete them and return the rest in a new array – sken boy Commented Sep 13, 2011 at 15:02
Add a comment  | 

4 Answers 4

Reset to default 9

Checkout the library underscore.js.

Say you have two arrays,

var a = [1, 2];
var b = [2, 3];

First find the union.

var all = _.union(a, b);

Then find the intersection.

var common = _.intersection(a, b);

The final answer should be the difference between the union, and the intersection.

var answer = _.difference(all, common)

Using Array.filter, Array.lastIndexOf, and Array.indexOf:

var array1 = [1,2,3,4,5];
var array2 = [2,3];
var unique = array1.concat(array2)
                   .filter(function (item, index, array) {
                       return array.indexOf(item) == array.lastIndexOf(item);
                   })

Neither method is 100% cross browser by default, but both links having safe shims for IE <= 8

Well, since you specified jQuery, try this:

var arr1 = [2, 3, 4];
var arr2 = [1, 2, 3];

var arr3 = $.merge($.grep(arr1, function(el, idx) {
    return $.inArray(el, arr2) > -1;
}, true), $.grep(arr2, function(el, idx) {
    return $.inArray(el, arr1) > -1;
}, true));

alert(arr3);

It's probably not very efficient, but it's relatively concise.

Plain js solution, not as efficient as when jQuery is used:

function filter(a1, a2){
  var result = [];
  for(i in a1){
    exists = false;
    for(j in a2){
      if(a1[i] == a2[j])
        exists = true;
    }
    if(exists== false){
      result.push(a1[i]);
    }
  }
  return result;
}

var arr1 = [1,2,3,4,5];
var arr2 = [4,5,6,7,8];
var result1 = filter(arr1, arr2);
var result2 = filter(arr2, arr1);

var result = result1.concat(result2);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论