Gaurav
Gaurav

Reputation: 8487

Getting unmatched values from 2 arrays in JavaScript?

Suppose we have 2 Arrays say :

 A [] => 1 2 3 4 5

 B [] => 1 2 7 4 5 

is there any method in jQuery which will give the unmatched values of 2 arrays in this case :

 Result [] => 3 7

Upvotes: 2

Views: 3615

Answers (6)

VisioN
VisioN

Reputation: 145408

Here is another solution for modern browsers (one-liners, yes!):

var a = [1, 2, 3, 4, 5];
var b = [1, 2, 7, 4, 5];

var result = a.concat(b).filter(function(el, i) {
    return (i < a.length ? b : a).indexOf(el) == -1;
});

DEMO: http://jsfiddle.net/6Na36/


If you wish to preserve index checking also, you can use this variant:

var a = [1, 2, 3, 4, 5];
var b = [1, 2, 7, 4, 5];

var result = a.concat(b).filter(function(el, i, c) {
    return el != c[i < a.length ? i + a.length : i - a.length];
});

DEMO: http://jsfiddle.net/6Na36/1/

Note, both variants successfully work with arrays of different size.

Upvotes: 0

xdazz
xdazz

Reputation: 160843

jQuery.inArray() will do some help:

var a = [1,2,3,4,5], b=[1,2,7,4,5];

var ret = 
a.filter(function(el) {
  return $.inArray(el, b) === -1;
}).concat(
b.filter(function(el) {
  return $.inArray(el, a) === -1;    
})
);
console.log(ret);

The demo.

PS: Or you could just use b.indexOf(el) === -1, then you don't need jQuery anymore.

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

function getUnique(A, B){
  var res = [];
  $.grep(A, function(element) {
    if($.inArray(element, B) == -1) res.push(element)        
  });
  $.grep(B, function(element) {
    if($.inArray(element, A) == -1) res.push(element);    
  });
  return res;
}

Use:

var A = [1,2,3,4,5],
    B = [1,2,3,5,7];

getUnique(A, B);

DEMO

Upvotes: 0

Tats_innit
Tats_innit

Reputation: 34107

hiya working demo here: http://jsfiddle.net/mbKfT/

good read http://api.jquery.com/jQuery.inArray/

This uses inArray to check it the element is there if not add it to intersect array.

rest demo will sue out any doubts :)

code

var a1 = [1,2,3,4,5];
var a2 = [1,2,7,4,5];
var intersect = [];

$.each(a1, function(i, a1val) {

    if ($.inArray(a1val, a2) === -1) {   
        intersect.push(a1val);
    }
});

$.each(a2, function(i, a1val) {

    if ($.inArray(a1val, a1) === -1) {           
        intersect.push(a1val);
    }
});
$("div").text(intersect);
alert(intersect + " -- " + matches);

​

Upvotes: 2

MaxArt
MaxArt

Reputation: 22617

var nomatch = [], Bcopy = B.slice(0);
for (var i = 0, j; i < A.length; i++) {
    j = Bcopy.indexOf(A[i]);
    if (j === -1) nomatch.push(A[i]);
    else Bcopy.splice(j, 1);
}
nomatch.push.apply(nomatch, Bcopy);

Note:

  1. This code supposes that items in A and B are unique.
  2. indexOf for arrays must be emulated in IE8 and previous versions.

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

Answer : no.

Solution : use a standard javascript loop.

var nomatches = [];
for (var i=Math.min(A.length, B.length); i-->0;) {
   if (A[i]!=B[i]) {
       nomatches.push(A[i]);
       nomatches.push(B[i]);
   }
}
// do what you want with remaining items if A.length != B.length

If, as supposed by a Rory, you don't want to match arrays but logical sets, you can do this :

 var nomatches = [];
var setA = {};
var setB = {};
for (var i=A.length; i-->0;) setA[A[i]]=1;
for (var i=B.length; i-->0;) setB[B[i]]=1;
for (var i=A.length; i-->0;) {
    if (!setB[A[i]]) nomatches.push(A[i]);
}
for (var i=B.length; i-->0;) {
    if (!setA[V[i]]) nomatches.push(B[i]);
}

Upvotes: 2

Related Questions