How to remove duplicate values from several arrays in JavaScript _ javascript skills

Source: Internet
Author: User
The following small series will provide you with a JavaScript array to remove duplicate values. I think it is quite good. Now I will share it with you and give you a reference. It is a common requirement to repeat arrays together with a small Editor. We will temporarily consider repeat arrays of the same type. It mainly aims to clarify the ideas and consider the performance. The following methods are available on the Internet. Here is a brief summary.

Ideas:

1. traverse the array, compare them one by one, and delete
2. traverse the array, compare them one by one, compare them to the same ones, skip the previous duplicate, and put the different ones into the new array.
3. Add an array element to the new array, traverse the remaining array element, and compare it with the elements of the new array one by one. If there are different elements, add the new array.
4. traverse the array and take an element as the object attribute to determine whether the attribute exists.

1. Delete the following duplicate:

Function ov1 (arr) {// var a1 = (new Date). getTime () for (var I = 0; I

2. This is a conventional method and is easy to understand. If the same method is used, the loop jumps out.

function ov2(a) {  //var a1=((new Date).getTime())  var b = [], n = a.length, i, j;  for (i = 0; i < n; i++) {    for (j = i + 1; j < n; j++)      if (a[i] === a[j]){j=false;break;}    if(j)b.push(a[i]);    }  //console.info((new Date).getTime()-a1)    return b.sort(function(a,b){return a-b});} 

3. It took me a long time to understand this. Although the j loop continues here, the I value has changed. It is like a new I loop:

function ov3(a) {  //var a1=((new Date).getTime())  var b = [], n = a.length, i, j;  for (i = 0; i < n; i++) {    for (j = i + 1; j < n; j++)    if (a[i] === a[j])j=++i  b.push(a[i]);}  //console.info((new Date).getTime()-a1)    return b.sort(function(a,b){return a-b});}   

4. Ensure that the new array is unique.

Function ov4 (ar) {// var a1 = (new Date). getTime () var m = [], f; for (var I = 0; I

5. Use object attributes

function ov5(ar){//  var a1=(new Date).getTime()var m,n=[],o= {};for (var i=0;(m= ar[i])!==undefined;i++)if (!o[m]){n.push(m);o[m]=true;}//  console.info((new Date).getTime()-a1)  return n.sort(function(a,b){return a-b});;}

The recommended method for removing repeated values in the above JavaScript arrays is all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference and support for the script.

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.