js数组中删除重复值的代码小结
时间:2021-12-19 10:49:59|栏目:JavaScript代码|点击: 次
js数组中去除重复值
[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
方法二
//去重复数组
function unique(data){
data = data || [];
var a = {};
len = data.length;
for (var i=0; i<len;i++){
var v = data[i];
if (typeof(a[v]) == 'undefined'){
a[v] = 1;
}
};
data.length=0;
for (var i in a){
data[data.length] = i;
}
return data;
}
方法三
var arr = ["123","123","123","123","sfsdf","123","345","123","123","345","456","567","sdc"];
var str = [];
for(var i = 0,len = arr.length;i < len;i++){
! RegExp(arr[i],"g").test(str.join(",")) && (str.push(arr[i]));
}
alert(str);
方法四
var pureMulti1=function(arr){
var obj={};
var a = [];
for(var i=0,l=arr.length;iif(!((arr[i]+"") in obj)){
a.push(arr[i]);
}
obj[arr[i]]="";
}
return a;
}
[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
方法二
复制代码 代码如下:
//去重复数组
function unique(data){
data = data || [];
var a = {};
len = data.length;
for (var i=0; i<len;i++){
var v = data[i];
if (typeof(a[v]) == 'undefined'){
a[v] = 1;
}
};
data.length=0;
for (var i in a){
data[data.length] = i;
}
return data;
}
方法三
复制代码 代码如下:
var arr = ["123","123","123","123","sfsdf","123","345","123","123","345","456","567","sdc"];
var str = [];
for(var i = 0,len = arr.length;i < len;i++){
! RegExp(arr[i],"g").test(str.join(",")) && (str.push(arr[i]));
}
alert(str);
方法四
复制代码 代码如下:
var pureMulti1=function(arr){
var obj={};
var a = [];
for(var i=0,l=arr.length;iif(!((arr[i]+"") in obj)){
a.push(arr[i]);
}
obj[arr[i]]="";
}
return a;
}
上一篇:JavaScript实现判断图片是否加载完成的3种方法整理
栏 目:JavaScript代码
本文标题:js数组中删除重复值的代码小结
本文地址:http://www.codeinn.net/misctech/187345.html