js生成两个数组的不同数组
function diffArray(arr1, arr2) {
var tmp = arr1.concat(arr2);
var arr3=[]; //临时数组
for(var s in arr1){
for(var x in arr2){
if(arr1[s]===arr2[x]){
arr3.push(arr1[s]);
}
}
}
tmp=tmp.filter(function(tmp){
if(arr3.indexOf(tmp)==-1){
return tmp
}
})
return tmp
}
//方法二
function diff(arr1, arr2) {
var newArr = arr1.concat(arr2);
function check(item) {
if (arr1.indexOf(item) === -1 || arr2.indexOf(item) === -1) {
return item;
}
}
return newArr.filter(check);
}
js数组去重
//方法一
Array.prototype.arrRdu= function()
{
this.sort();
var re=[this[0]];
for(var i = 1; i < this.length; i++)
{
if( this[i] !== re[re.length-1])
{
re.push(this[i]);
}
}
return re;
}
//方法二
Array.prototype.arrRdu= function()
{
var n = {}, r = [], len = this.length, val, type;
for (var i = 0; i < this.length; i++) {
val = this[i];
type = typeof val;
if (!n[val]) {
n[val] = [type];
r.push(val);
} else if (n[val].indexOf(type) < 0) {
n[val].push(type);
r.push(val);
}
}
return r;
}