数组去重
var arr = [0,0,1,1,1,2,2,3,3,5,5,7,7,3,3,'q','q','cc',1];
Array.prototype.unique = function (){
var temp = {},
newArr = [];
for(var i = 0; i < this.length; i++){
if(!temp.hasOwnProperty(this[i])){
temp[this[i]] = this[i];
newArr.push(this[i]);
}
}
return newArr;
};
console.log(arr.unique());//[0, 1, 2, 3, 5, 7, "q", "cc"]
字符串去重
// var str = 'aabbccdd1122334455667788';
// String.prototype.unique = function (){
// var temp = {},
// newStr = [];
// for(var i = 0; i < this.length; i++){
// if(!temp.hasOwnProperty(this[i])){
// temp[this[i]] = this[1];
// newStr += this[i];
// }
// }
// return newStr;
// };
// console.log(str.unique());