用JavaScript脚本为Array对象添加一个去除重复项的方法。
}
Array.prototype.distict=function(){ var result=[],flag=false; for(var i=0;i<this.length;i++){ if(this[i]!=this[i]){ if(flag){ continue; }else{ flag=true; result.push(this[i]); } }else if(result.indexOf(this[i])==-1){ result.push(this[i]); } } return result; }; var arr=[2,null,3,4,NaN,2,3,6,8,4,NaN,undefined,undefined,null,6]; //[2, null, 3, 4, NaN, 6, 8, undefined]
Array.prototype.removeDulplicates = function () { for (var i = 0; i < this.length;) { removeDulplicatesFromArray(this, this[i], ++i); } function removeDulplicatesFromArray(arr, item, start) { var index = arr.indexOf(item, start); if (index != -1) { arr.splice(index, 1); removeDulplicatesFromArray(arr, item, index); } } };