JS去除数组中重复值的四种方法
1 /// <summary>
2 /// 回车键按钮事件(此方法需要在页面加载完后执行,比如在Jquery中的ready中调用)--方法一
3 /// </summary>
4 /// 调用示例:
5 /// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
6 /// var v_ArrayResult = v_Array.deleteEle();
7 /// alert(v_ArrayResult); //返回结果:, 2, 3, 4, 5, 6, 7
8 Array.prototype.deleteEle = function () {
9 var o = {}, newArr = [], i, j;
10 for (i = 0; i < this.length; i++) {
11 if (typeof (o[this[i]]) == "undefined") {
12 o[this[i]] = "";
13 }
14 }
15 for (j in o) {
16 newArr.push(j)
17 }
18 return newArr;
19 }
20 /// <summary>
21 /// 回车键按钮事件(此方法需要在页面加载完后执行,比如在Jquery中的ready中调用)--方法二
22 /// </summary>
23 /// 调用示例:
24 /// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
25 /// var v_ArrayResult = v_Array.deleteEleReg();
26 /// alert(v_ArrayResult); //返回结果:, 2, 3, 4, 5, 6, 7
27 Array.prototype.deleteEleReg = function () {
28 return this.reverse().join(",").match(/([^,]+)(?!.*\1)/ig).reverse();
29 }
30 /// <summary>
31 /// 回车键按钮事件(此方法需要在页面加载完后执行,比如在Jquery中的ready中调用)--方法三
32 /// </summary>
33 /// <param name="array">数组</param>
34 /// 调用示例:
35 /// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
36 /// alert(unique(v_Array)); //返回结果:, 2, 3, 4, 5, 6, 7
37 function deleteEleFunction(array) {
38 var ret = [], record = {}, it, tmp, obj = "__object__", bak = [], i, len;
39 var type = {
40 "number": function (n) { return "__number__" + n; },
41 "string": function (n) { return "__string__" + n; },
42 "boolean": function (n) { return "__boolean__" + n; },
43 "undefined": function (n) { return "__undefined__"; },
44 "object": function (n) {
45 return n === null ? "__null__" : obj in n ? n[obj] : (n[obj] = obj + bak.push(n));
46 }
47 };
48 for (i = 0, len = array.length; i < len; i++) {
49 it = array[i]; tmp = type[typeof it](it);
50 if (!(tmp in record)) { ret.push(it); record[tmp] = true; }
51 }
52 for (i = 0, len = bak.length; i < len; delete bak[i++][obj]) { }
53 return ret;
54 };
55
56 /// <summary>< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />
57 /// 回车键按钮事件(此方法需要在页面加载完后执行,比如在Jquery中的ready中调用)--方法四
58 /// </summary>
59 /// 调用示例:
60 /// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
61 /// var v_ArrayResult = v_Array.deleteEleDis();
62 /// alert(v_ArrayResult); //返回结果:, 2, 3, 4, 5, 6, 7
63 Array.prototype.deleteEleDis = function () {
64 var a = [], b = [];
65 for (var prop in this) {
66 var d = this[prop];
67 if (d === a[prop]) continue; //防止循环到prototype
68 if (b[d] != 1) {
69 a.push(d);
70 b[d] = 1;
71 }
72 }
73 return a;
74 }
3 /// </summary>
4 /// 调用示例:
5 /// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
6 /// var v_ArrayResult = v_Array.deleteEle();
7 /// alert(v_ArrayResult); //返回结果:, 2, 3, 4, 5, 6, 7
8 Array.prototype.deleteEle = function () {
9 var o = {}, newArr = [], i, j;
10 for (i = 0; i < this.length; i++) {
11 if (typeof (o[this[i]]) == "undefined") {
12 o[this[i]] = "";
13 }
14 }
15 for (j in o) {
16 newArr.push(j)
17 }
18 return newArr;
19 }
20 /// <summary>
21 /// 回车键按钮事件(此方法需要在页面加载完后执行,比如在Jquery中的ready中调用)--方法二
22 /// </summary>
23 /// 调用示例:
24 /// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
25 /// var v_ArrayResult = v_Array.deleteEleReg();
26 /// alert(v_ArrayResult); //返回结果:, 2, 3, 4, 5, 6, 7
27 Array.prototype.deleteEleReg = function () {
28 return this.reverse().join(",").match(/([^,]+)(?!.*\1)/ig).reverse();
29 }
30 /// <summary>
31 /// 回车键按钮事件(此方法需要在页面加载完后执行,比如在Jquery中的ready中调用)--方法三
32 /// </summary>
33 /// <param name="array">数组</param>
34 /// 调用示例:
35 /// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
36 /// alert(unique(v_Array)); //返回结果:, 2, 3, 4, 5, 6, 7
37 function deleteEleFunction(array) {
38 var ret = [], record = {}, it, tmp, obj = "__object__", bak = [], i, len;
39 var type = {
40 "number": function (n) { return "__number__" + n; },
41 "string": function (n) { return "__string__" + n; },
42 "boolean": function (n) { return "__boolean__" + n; },
43 "undefined": function (n) { return "__undefined__"; },
44 "object": function (n) {
45 return n === null ? "__null__" : obj in n ? n[obj] : (n[obj] = obj + bak.push(n));
46 }
47 };
48 for (i = 0, len = array.length; i < len; i++) {
49 it = array[i]; tmp = type[typeof it](it);
50 if (!(tmp in record)) { ret.push(it); record[tmp] = true; }
51 }
52 for (i = 0, len = bak.length; i < len; delete bak[i++][obj]) { }
53 return ret;
54 };
55
56 /// <summary>< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />
57 /// 回车键按钮事件(此方法需要在页面加载完后执行,比如在Jquery中的ready中调用)--方法四
58 /// </summary>
59 /// 调用示例:
60 /// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
61 /// var v_ArrayResult = v_Array.deleteEleDis();
62 /// alert(v_ArrayResult); //返回结果:, 2, 3, 4, 5, 6, 7
63 Array.prototype.deleteEleDis = function () {
64 var a = [], b = [];
65 for (var prop in this) {
66 var d = this[prop];
67 if (d === a[prop]) continue; //防止循环到prototype
68 if (b[d] != 1) {
69 a.push(d);
70 b[d] = 1;
71 }
72 }
73 return a;
74 }