Metode 1: Ulangi Data
Salin kode kode sebagai berikut:
<script>
Array.prototype.distinct = function () {
var a = [], b = [];
untuk (var prop di ini) {
var d = ini [prop];
if (d === a [prop]) Lanjutkan; // Cegah sirkulasi ke prototipe
if (b [d]! = 1) {
A.Push (D);
b [d] = 1;
}
}
Mengembalikan a;
}
var x = ['a', 'b', 'c', 'd', 'b', 'a', 'e', 'a', 'b', 'c', 'd', 'B,' B, 'B', 'A', 'E'];
Document.write ('Array Asli:'+x);
document.write ("<br />");
document.write (setelah mengulang: '+x.distinct ());
</script>
Metode Dua: Ambil Data Duplikat
Salin kode kode sebagai berikut:
<type skrip = "Teks/JavaScript">
Array.prototype.distinct = function () {
var a = [], b = [], c = [], d = [];
untuk (var prop di ini) {
var d = ini [prop];
if (d === a [prop])
{{
melanjutkan;
} // mencegah sirkulasi ke propotipe
if (b [d]! = 1) {
A.Push (D);
b [d] = 1;
}
kalau tidak {
C.Push (D);
d [d] = 1;
}
}
// kembalikan a;
Return c.distinct1 ();
}
Array.prototype.distint1 = function () {
var a = [], b = [];
untuk (var prop di ini) {
var d = ini [prop];
if (d === a [prop]) Lanjutkan; // Cegah sirkulasi ke prototipe
if (b [d]! = 1) {
A.Push (D);
b [d] = 1;
}
}
Mengembalikan 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 ('Array Asli:'+x);
document.write ("<br />");
document.write (setelah mengulang: '+x.distinct ());
</script>