JavaScript 去掉数组中的重复元素[转]
时间:2010-08-11 来源:wlhgbs2008
Js代码1 function unique(data)
2 {
3 data = data || [];
4 var a = {};
5 for (var i = 0, len = data.length; i < len; i++)
6 {
7 var v = data[i];
8 if ('undefined' == typeof(a[v]))
9 {
10 a[v] = 1;
11 }
12 };
13 data.length = 0;
14 for (var i in a)
15 {
16 data[data.length] = i;
17 }
18
19 return data;
20 }
参考 $.cookie(); 搞成 jQuery 插件
10 jQuery.unique_array = function(data)
11 {
12 data = data || [];
13 var a = {};
14 for (var i = 0, len = data.length; i < len; i++)
15 {
16 var v = data[i];
17 if ('undefined' == typeof(a[v]))
18 {
19 a[v] = 1;
20 }
21 }
22
23 data.length = 0;
24 for (var i in a)
25 {
26 data[data.length] = i;
27 }
28
29 return data;
30 }
2 {
3 data = data || [];
4 var a = {};
5 for (var i = 0, len = data.length; i < len; i++)
6 {
7 var v = data[i];
8 if ('undefined' == typeof(a[v]))
9 {
10 a[v] = 1;
11 }
12 };
13 data.length = 0;
14 for (var i in a)
15 {
16 data[data.length] = i;
17 }
18
19 return data;
20 }
参考 $.cookie(); 搞成 jQuery 插件
10 jQuery.unique_array = function(data)
11 {
12 data = data || [];
13 var a = {};
14 for (var i = 0, len = data.length; i < len; i++)
15 {
16 var v = data[i];
17 if ('undefined' == typeof(a[v]))
18 {
19 a[v] = 1;
20 }
21 }
22
23 data.length = 0;
24 for (var i in a)
25 {
26 data[data.length] = i;
27 }
28
29 return data;
30 }
相关阅读 更多 +