Javascript removes repetitive elements from arrays _javascript tips

Source: Internet
Author: User
I opened the Firebug console, took 30 seconds to write 30 seconds, and the results were as follows: (x is the target array, y is the result of removing duplicate elements)
Copy Code code as follows:

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 ());

The answer is not unique, and there are no standard answers, so the above code is right and wrong. What if the array is a complex array of objects? What if the array contains more than one empty object {}? To know that JS has a lot of special or even bug in the phenomenon, alert ({}=={}) to see what's going on ...

Note: If anyone has a standard answer welcome comments inside Send let everyone learn to learn.

The question-makers themselves will not take these questions into account.

Do not learn to do the problem, in the actual work encountered problems when the ability to solve problems effectively is truly valuable.

Places to be noted for additional:

There's a problem to be aware of.
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

My example is an array of number types, which is designed to express an attitude about what to do in an interview. Practical application should be considered according to the actual needs. Unless the actual work required or I am a JS academic researcher, otherwise I will not be independent to implement a equals method. Foreign developers tend to follow a credo: "Do the simplest Thing that could possibly Work", meaning "don't overdo, apply first", my work experience of these years also often confirms this truth. I don't know what you think.
Related Article

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.