Home > Web Front-end > JS Tutorial > Four different ways to remove duplicate values ​​from an array in JS_javascript skills

Four different ways to remove duplicate values ​​from an array in JS_javascript skills

WBOY
Release: 2016-05-16 17:23:16
Original
1118 people have browsed it
Copy code The code is as follows:

/// 2 /// Enter key button Event (this method needs to be executed after the page is loaded, such as calling in ready in Jquery)--Method 1
///

/// Calling example:
// / 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); //Return results:, 2, 3, 4, 5, 6, 7
Array.prototype.deleteEle = function () {
var o = {}, newArr = [], i, j;
for (i = 0; i < this.length; i ) {
if (typeof (o[this[i]]) == "undefined") {
o[this[i]] = "";
}
}
for (j in o) {
newArr.push(j)
}
return newArr;
}
///
/// Enter key button event (this method needs to be executed after the page is loaded, such as calling in ready in Jquery )--Method 2
///

/// Calling example:
/// 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); //Return the result :, 2, 3, 4, 5, 6, 7
Array.prototype.deleteEleReg = function () {
return this.reverse().join(",").match(/([^, ] )(?!.*1)/ig).reverse();
}
///
/// Enter key button event (this method needs to be loaded after the page Executed later, such as calling in ready in Jquery)--Method 3
///

/// array
/// Calling example:
/// 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)); //Return results:, 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 < len; 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 < len; delete bak[i ][obj]) { }
return ret;
};

/// < summary>< For example, called in ready in Jquery)--Method 4
///
/// Calling example:
/// 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); //Return results:, 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; //Prevent looping to prototype
if (b[d ] != 1) {
a.push(d);
b[d] = 1;
}
}
return a;
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template