English 中文(简体)
正在从 JavaScript 中的 2 个数组获得不匹配的值吗?
原标题:Getting unmatched values from 2 arrays in JavaScript?

假设我们有两个阵列说:

 A [] => 1 2 3 4 5

 B [] => 1 2 7 4 5 

jQuery 中存在任何方法,在此情况下给出两个数组的不匹配值 :

 Result [] => 3 7
最佳回答

这里的“强力”工作演示: http://jsfidd.net/mbKfT/

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

这里使用 inArray 来检查元素是否添加到 interect 阵列中 。

休息演示会起诉任何疑问 :)

<强度 > 代码

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

​
问题回答

答复:无。

解决方案 : 使用标准的 javascript 环 。

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

如果,如 Rory 所假设的那样, 您不想匹配数组, 但符合逻辑, 您可以做到这一点 :

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

注:

  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.inAray () 将帮助:

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

http://jsfidle.net/ATAUq/" rel="not follow" >demo 。

PS: 或者你只要使用 b.indexof(el)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

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

使用 :

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

getUnique(A, B);

http://jsfidd.net/ZjqyU/1/" rel=“notfollow” >DEMO

这是现代浏览器的另一种解决办法(单线浏览器, 是! ) :

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/


如果您也想要保存索引检查, 您可以使用此变量 :

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/

注意,两个变量都与不同大小的阵列成功合作。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.