How to remove repeated elements from arrays using Javascript _ javascript

Source: Internet
Author: User
In js, how does one remove repeated elements in a specified array? I opened the firebug console and spent 30 seconds thinking, writing 30 seconds. The result is as follows: (x is the target array, and y is the result after repeated elements are removed)

The Code is as follows:


Var x = [1, 1, 3, 4, 5, 3];
Var y = [];
Var tArray = function (I, arr ){
Var yap = false;
For (var j = 0; jif (arr [j] = I) {yap = true; break ;};
}
If (! Yap) arr. push (I );
};
For (var t = 0; t TArray (x [t], y );
}
Alert (y. length );
Alert (y. toString ());


The answer is not unique and there is no standard answer, so the above Code is correct and wrong. What if the array is a complex object array? What if the array contains multiple null objects? You need to know that js contains many special and even bug phenomena. alert ({}={}) to see what is going on...

Note: if anyone has a standard answer, please post it in the comments for everyone to learn.

The problem will not be taken into account by the author.

Do not study and do questions. The ability to efficiently solve problems in actual work is truly valuable.

Note the following:

Pay attention to the following issues:
Arr [j] = I you seem to be referring to the Equality judgment when the array element is of the number type...
However, we may need to make equality judgments for different types of applications.

1! = New Number (1)
Null = undefined

0 = "0" and so on

Therefore, the simple = and = are not suitable.
An equals method should be implemented independently for Equality judgment... rules should be set as needed

The example is for the number array to express an attitude towards "Interview Questions. The actual application should be considered based on actual needs. Unless required in actual work or I am a js academic researcher, I will not implement an equals method independently. Developers outside China often follow The principle: "Do The Simplest Thing That cocould Possibly Work", which means "Do not over-design, apply first ", my work experience over the past few years often proves this truth. I don't know what you think.

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.