Array.prototype.distinct = function () { var ret = []; for (var i = 0; i < this.length; i++) { for (var j = i + 1; j < this.length; j++) { if (this[i] === this[j]) { ret.push(this.splice(j, 1)[0]); } } } return this; //返回去重后的数组 // return ret;//返回重复的数组 } var arrayA = ['ab', 'bb', 'bc', 'd', 'ab', 'e', 'd', 'c', 'a']; alert(arrayA.distinct());
Array.prototype.uniq = function () {var k=false;for(var i=0;i<this.length;i++){if(this[i]!==this[i]){k=true;}for(var j=i+1;j<this.length;){if(this[i]===this[j]||(k && this[j]!==this[j])){this.splice(j,1);}else{j++;}}}returnthis;}可以去除NaN null undefined