Method 1: Repeat data
Copy code code as follows:
<script>
Array.prototype.distinct = Function () {
var a = [], b = [];
for (var prop in this) {
var d = this [prop];
if (d === a [prop]) Continue; // Prevent circulation to Prototype
if (b [d]! = 1) {
a.push (d);
b [d] = 1;
}
}
Return a;
}
var x = ['a', 'b', 'c', 'd', 'b', 'a', 'e', 'a', 'b', 'c', 'd', 'b,' b, 'b ',' a ',' e '];
document.write ('original array:'+x);
document.write ("<br />");
document.write (after repeating: '+x.distinct ());
</script>
Method two: Take duplicate data
Copy code code as follows:
<script type = "text/javascript">
Array.prototype.distinct = Function () {
var a = [], b = [], c = [], d = [];
for (var prop in this) {
var d = this [prop];
if (d === a [prop])
{{
continue;
} // Prevent circulation to propotype
if (b [d]! = 1) {
a.push (d);
b [d] = 1;
}
else {
c.push (d);
d [d] = 1;
}
}
// Return a;
Return c.distinct1 ();
}
Array.prototype.distInt1 = Function () {
var a = [], b = [];
for (var prop in this) {
var d = this [prop];
if (d === a [prop]) Continue; // Prevent circulation to Prototype
if (b [d]! = 1) {
a.push (d);
b [d] = 1;
}
}
Return a;
}
var x = ['a', 'b', 'c', 'd', 'b', 'a', 'e', 'a', 'b', 'c', 'd', 'b,' b, 'b 'a' a ',' e ',' f ',' f ',' g '];
document.write ('original array:'+x);
document.write ("<br />");
document.write (after repeating: '+x.distinct ());
</script>