日期:2014-05-16  浏览次数:20466 次

js去除重复元素

?var arr=[{"name":'tangchao',id:123,age:23},{name:'tangchao',id:123,age:25}

,{name:'tangchao',id:124,age:26},{name:'tangchao',id:124,age:27},{name:'tangchao',id:123,age:28},

{name:'tangchao',id:125,age:29},{name:'tangchao',id:125,age:30},

{name:'tangchao1',id:126,age:29},{name:'tangchao1',id:126,age:30}];

?console.debug(arr.distinctRepeatArray({id:'id',name:'name'}));

?

Array.prototype.distinctRepeatArray=function(ob){

var res = [], hash = {};

for ( var i = 0, elem; i<this.length &&(elem = this[i][ob.id]) != null; i++) {

if (!hash[elem]) {

res.push({id:elem,name:this[i][ob.name]});

hash[elem] = true;

}

}

return res;

}