JS去除数组重复值的四种不同方法_javascript技巧
程序员文章站
2022-04-28 13:46:01
...
复制代码 代码如下:
///
///
/// 调用示例:
/// 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, 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;
}
推荐阅读
-
js数组中去除重复值的几种方法
-
js数组中删除重复值的代码小结_javascript技巧
-
js数组中删除重复值的代码小结_javascript技巧
-
js过滤数组重复元素的方法_javascript技巧
-
JavaScript去除数组里重复值的方法_javascript技巧
-
使用不同的方法结合/合并两个JS数组_javascript技巧
-
JavaScript中去掉数组中的重复值的实现方法_javascript技巧
-
js 两数组去除重复数值的实例_javascript技巧
-
JavaScript中去掉数组中的重复值的实现方法_javascript技巧
-
JS实现合并两个数组并去除重复项只留一个的方法_javascript技巧