【紧急求助】JS如何取两个数组 不相同的值
var a = new Array("1","2","3","4","5","6","7",);
var b = new Array("1","2","5",);
var c = new Array();
var index = 0;
现在要将 a 和b 数组不相同的值取出来放到c 中,怎么处理啊,求指教;
for(var i=0;i<a.length;i++){
for(var j=0;j<b.length;j++){
if(a[i] != b[j]){
c[index++] = b[j];
}
}
}我大概是这样弄的,但这样弄有很多重复的,求一个简单方便的 好办法
------解决方案--------------------var a=[1,2,3,4,5];
var b=[1,2,3,7];
var c=[];
for(var i in a){
c[a[i]]=a[i];
}
for(var i in b){
if(c[b[i]]){
delete c[b[i]];
}else{
c[b[i]]=b[i];
}
}
for(var i in c){
document.write(c[i]+",");
}
这样试试
------解决方案--------------------
JScript code
var a = [1,2,3,4,5,6,7];
var b = [1,2,5];
var c = [];
var tmp = a.concat(b);
var o = {};
for (var i = 0; i < tmp.length; i ++) (tmp[i] in o) ? o[tmp[i]] ++ : o[tmp[i]] = 1;
for (x in o) if (o[x] == 1) c.push(x);
alert(c);