首頁 >web前端 >js教程 >JS去除數組重複值的四種不同方法_javascript技巧

JS去除數組重複值的四種不同方法_javascript技巧

WBOY
WBOY原創
2016-05-16 17:23:161106瀏覽
複製程式碼 程式碼如下:

/// 2 /// 回車鍵事件按鈕事件(此方法需要在頁面載入完後執行,例如在Jquery中的ready中呼叫)--方法一
///

/// 呼叫範例:
/// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
/// var v_ArrayResult = v_Array.deleteEle ();
/// alert(v_ArrayResult); //回傳結果:, 2, 3, 4, 5, 6, 7
Array.prototype.deleteEle = function () {
var o = {}, newArr = [], i, j;
for (i = 0; i if (typeof (o[this[i]]) == "undefined" ) {
o[this[i]] = "";
}
}
for (j in o) {
newArr.push(j)
}
return newArr;
}
///
/// 回車鍵按鈕事件(此方法需要在頁面載入完後執行,例如在Jquery中的ready中呼叫)--方法二
///

/// 呼叫範例:
/// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
/// var v_ArrayResult = v_Array.deleteEleReg();
/// alert(v_ArrayResult); //回傳結果:, 2, 3 , 4, 5, 6, 7
Array.prototype.deleteEleReg = function () {
return this.reverse().join(",").match(/([^,] )(?! .*1)/ig).reverse();
}
///
/// 回車鍵按鈕事件(此方法需要在頁面載入完後執行,例如在Jquery中的ready中呼叫)--方法三
///

/// 陣列
/// 呼叫範例:
/// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2, 4, 2, 1, 1, 3);
/// alert (unique(v_Array)); //回傳結果:, 2, 3, 4, 5, 6, 7
function deleteEleFunction(array) {
var ret = [], record = {}, it, tmp}, it, tmp} , obj = "__object__", bak = [], i, len;
var type = {
"number": function (n) { return "__number__" n; },
"string": function (n) { return "__string__" n; },
"boolean": function (n) { return "__boolean__" n; },
"undefined": function (n) { return "__undefined__"; },
"object": function (n) {
return n === null ? "__null__" : obj in n ? n[obj] : (n[obj] = obj bak.push(n) );
}
};
for (i = 0, len = array.length; i it = array[i]; tmp = type[typeof it] (it);
if (!(tmp in record)) { ret.push(it); record[tmp] = true; }
}
for (i = 0, len = bak.length ; i return ret;
};

///
/// 回車鍵按鈕事件(此方法需要在頁面載入完後執行,例如在Jquery中的ready中呼叫)--方法四
///

/// 呼叫範例:
/// var v_Array = new Array(1, 2, 3, 4, 5, 6, 7, 3, 3, 2, 2 , 4, 2, 1, 1, 3);
/// var v_ArrayResult = v_Array.deleteEleDis();
/// alert(v_ArrayResult); //傳回結果:, 2, 3, 4, 5, 6, 7
Array.prototype.deleteEleDis = function () {
var a = [], b = [];
for (var prop in this) {
var d = this [prop];
if (d === a[prop]) continue; //防止循環到prototype
if (b[d] != 1) {
a.push(d);
b[d] = 1;
}
}
return a;
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn