vararray= [
{a:1,b:2,c:3,d:4},
{a:11,b:22,c:333,d:44},
{a:111,b:222,c:333,d:444},
{a:11,b:22,c:33,d:44},
{a:11,b:22,c:33,d:444}
];
//将对象元素转换成字符串以作比较
functionobj2key(obj,keys){
varn=keys.length,
key= [];
while(n--){
key.push(obj[keys[n]]);
}
returnkey.join('|');
}
//去重操作
functionuniqeByKeys(array,keys){
vararr= [];
varhash= {};
for(vari=0,j=array.length;i
vark=obj2key(array[i],keys);
if(!(kinhash)) {
hash[k] =true;
arr.push(array[i]);
}
}
returnarr;
}
//复制代码
//进行去重
vararr=uniqeByKeys(array,['a','b']);
console.log(JSON.stringify(arr))
网友评论