数组去重
//数组元素去重
//①使用对象的属性名不能重复
//缺点:无法分辨2和“2”
| if(!Array.prototype.delRepeat){ Array.prototype.delRepeat = function(){ var result =[]; var temp = {}; for(var i=0;i<this.length;i++){ if(!temp[this[i]]){ result.push(this[i]); temp[this[i]] = true; } } return result; } } |
//②在方法①的基础上改进为能分辨基础类型的区别
//此方法用typeof来检测类型,作为对象的名的前缀,但是,如果是复合类型,object的toString()得到[object,Object];
| if(!Array.prototype.delRepeat_tp){ Array.prototype.delRepeat_tp = function(){ var result =[]; var temp = {}; for(var i=0;i<this.length;i++){ if(!temp[typeof(this[i])+this[i].toString()]){ result.push(this[i]); temp[typeof(this[i])+this[i].toString()] = true; } } return result; } } |
浙公网安备 33010602011771号