How JavaScript removes duplicate values from an array

Source: Internet
Author: User

How JavaScript removes duplicate values from the array:

Arrays are used to organize data, and sometimes the data is duplicated, and here's how to remove duplicate content from the array.

The code example is as follows:

varthearray=[1,2,3,4,5,6,4,3,10];functionUnique (data) {vardata=data| |[]; varA={};  for(vari=0;i<data.length;i++){    varv=Data[i]; if(typeof(A[v]) = = ' undefined ') {A[v]=1; }} data.length=0;  for(varIincha) {Data[data.length]=i; }  returndata;} Console.log (Unique (TheArray));

The above code can output an array element after removing duplicate content. Of course there are many ways to implement this feature, and this section describes only this one, which is the implementation process described below.

Implementation principle:

The principle is actually very simple, first establish an empty object A, and then iterate through each element in the array, and the element value of the array as an attribute of object A, if this property does not exist, and then add this property for object A and assign a value of 1, so that the non-repeating array elements become object a properties, and finally through the for The in element iterates through each property and adds it to the array so that it implements the function we want.

The original address is: http://www.51texiao.cn/javascriptjiaocheng/2015/0522/2247.html

The most original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9273

How JavaScript removes duplicate values from an array

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.