admin管理员组

文章数量:1389762

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

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
Share Improve this question edited May 26, 2012 at 12:36 VisioN 145k34 gold badges287 silver badges289 bronze badges asked May 26, 2012 at 10:54 GauravGaurav 8,49714 gold badges57 silver badges91 bronze badges 2
  • 1 A loop is so easy and fast, why are you looking for a jquery method ? Don't add useless layers. – Denys Séguret 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] – frenchie Commented May 26, 2012 at 11:03
Add a ment  | 

6 Answers 6

Reset to default 2

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

good read http://api.jquery./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);

​

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

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.

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

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/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/6Na36/1/

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

本文标签: jqueryGetting unmatched values from 2 arrays in JavaScriptStack Overflow