数组去重:
Array.prototype.unique = function(){ //利用了对象属性名的唯一性 var obj = {}, len = this.length, newArr = []; for(var i =0;i<len;i++) { if(!obj[this[i]]){ obj[this[i]] = i+1; newArr.push(this[i]); } } return newArr; }
对象数组去重:
Array.prototype.unique = function(key){ //利用了对象属性名的唯一性 var obj = {}, len = this.length, newArr = []; for(var i =0;i<len;i++) { if(!obj[this[i][key]]){ newArr.push(this[i]); obj[arr[i][key]] = true; } } return newArr; }