JavaScript Remove duplicate Array entries method and principle

Source: Internet
Author: User

Arr[j]==i you seem to refer to the equality judgment when an array element is a number type ...
But often we may have to make equality judgments about different types of ... This is the practical application of the problem should be considered

Case of 1!=new number (1)
The situation of null==undefined

0== "0" situation and so on

So obviously simple = = and = = are not suitable.
A Equals method should be implemented independently to make equality judgments ... To set rules based on requirements

<script>
Array.prototype.del = function () {
var a = {}, c = [], L = this.length;
for (var i = 0; i < L; i++) {
var b = this[i];
var d = (typeof B) + B;
if (a[d] = = undefined) {
C.push (b);
A[D] = 1;
}
}
return C;
}
alert ([1, 1, 2, 3, 4, 5, 4, 3, 4, 4, 5, 5, 6, 7].del ());
</script>

Method Two

var arr = ["123", "123", "123", "123", "SFSDF", "123", "345", "123", "123", "345", "456", "567", "SDC"];
var str = [];
for (var i = 0,len = Arr.length;i < len;i++) {
! RegExp (Arr[i], "G"). Test (Str.join (",")) && (Str.push (arr[i));
}
alert (str);

Method Three

var x=[1,1,3,4,5,3];
var y=[];
var tarray=function (I,arr) {
var Yap=false;
for (Var j=0;j<arr.length;j++) {
if (arr[j]==i) {yap=true;break;};
}
if (!yap) Arr.push (i);
};
for (Var t=0;t<x.length;t++) {
Tarray (X[t],y);
}
alert (y.length);
Alert (y.tostring ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.