2

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
2
  • 1
    A loop is so easy and fast, why are you looking for a jquery method ? Don't add useless layers. Commented May 26, 2012 at 10:57
  • If both lists contain unique value, you could merge them both. Then, sort the array, loop and find duplicate where element i === i+1 and then remove both of these elements. You'll be left with the array [3,7] Commented May 26, 2012 at 11:03

6 Answers 6

2

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

​
Sign up to request clarification or add additional context in comments.

Comments

2

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

3 Comments

+1 as it answers the OPs question directly, but what about cases where the arrays are in different orders. Consider 5,4,3,2,1 and 1,2,3,4,5.
It depends on the requirement.
This is not correct. Think of A={1,2,3,4,5}; B={1,3,4,5}. With your approach 3,4 and 5 would be in nomatches.
1
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.

Comments

1

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.

1 Comment

Why using inArray when you can use a.indexOf? It's good if dystroy already uses jQuery, but what if he doesn't?
0
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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.