日期:2014-05-16 浏览次数:20392 次
<script language="JavaScript"> <!-- Array.prototype.distinct = function(field) { var set = {}, hasField = typeof(field)!='undefined'; for(var i=this.length-1; i>=0; i--){ var obj = this[i], cacheKey = hasField ? obj[field] : obj; if(cacheKey in set){ this.splice(i, 1); continue; }else{ set[cacheKey] = null; } } } //test var a = [{id:1, name:'aaa'}, {id:2, name:'bbb'}, {id:1, name:'ccc'}]; a.distinct('id'); var b = [1,2,3,'a','b',2,'b',3]; b.distinct(); alert(a); alert(b); //--> </script>