
1,重复删除js数组的第一种也是最愚蠢的方法.
Array.prototype.unique1 = function () {
var r = new Array();
label:for(var i = 0, n = this.length; i < n; i++) {
for(var x = 0, y = r.length; x < y; x++) {
if(r[x] == this[i]) {
continue label;
}
}
r[r.length] = this[i];
}
return r;
}
2,使用正则表达式的js数组重复数据删除的第二种方法.

Array.prototype.unique2 = function () {
return this.sort().join(",,").replace(/(,|^)([^,]+)(,,\2)+(,|$)/g,"$1$2$4").replace(/,,+/g,",").replace(/,$/,"").split(",");
}
3,js数组重复数据删除的第三种方法:
使用对象的[hasOwnProperty]方法

Array.prototype.unique3 = function() {
var temp = {}, len = this.length;
for(var i=0; i < len; i++) {
var tmp = this[i];
if(!temp.hasOwnProperty(tmp)) {
temp[this[i]] = "my god";
}
}
len = 0;
var tempArr=[];
for(var i in temp) {
tempArr[len++] = i;
}
return tempArr;
}
4js array去重复,js数组重复数据删除的第四种方法: 首先排序,第一项要比最后一项好. 简单实用.
Array.prototype.unique4 = function () {
var temp = new Array();
this.sort();
for(i = 0; i < this.length; i++) {
if( this[i] == this[i+1]) {
continue;
}
temp[temp.length]=this[i];
}
return temp;
}

5,js数组重复数据删除的第五种方法js array去重复,有点像哈希表.
Array.prototype.unique5 = function() {
var res = [], hash = {};
for(var i=0, elem; (elem = this[i]) != null; i++) {
if (!hash[elem])
{
res.push(elem);
hash[elem] = true;
}
}
return res;
}
哪个适合您?谁是您一百英里的命运,注定要看到您的选择,哈.

我希望上述五种js数组重复数据删除方法对每个人都有用.
您可能感兴趣的文章:
在js数组中删除重复项的五种方法
在js数组中删除重复值的四种方法
php 2D阵列重复数据删除自定义功能
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-158942-1.html
关键是
那就是具有单身未婚的女青年的身份
7万亿放水