Use JavaScript to remove duplicates in an array

Source: Internet
Author: User
Tags object object

Using JavaScript's properties of object, we can easily implement an array of duplicates to be removed.

The attribute of object is: key must be unique.

Remove the array duplicates:

1 Convert an array to an object, the value of the array as the key of the object

Because key is unique, object does not add a key when it encounters a duplicate array value

2 Convert Object object to array, key is the value of the array.

In the previous repeating array, each value actually corresponds to an object with only one key, so that when you revert to the array, the duplicate value is removed

1 /*Remove duplicates of an array with the attributes of object:2 1 Convert an array into an Oject object, using the value of the array as the key of obj3 2 Convert an object to an array4             */5             6             //passing in an array to return object objects7             varToobj =function(arr) {8obj = {};9                  for(varTempincharr) {Ten                     //Each value of arr is added to the key,value of obj is true OneObj[arr[temp]] =true; A                 } -                 returnobj; -             }; the             //Incoming obj object returns an array of arr -             varToarr =function(obj) { -                 vararr = []; -                  for(varTempinchobj) { +                     //push all the keys into the array - Arr.push (temp); +                 } A                 returnarr; at             }; -             //unite the two methods. -             varGetuniq =function(arr) { -                 returnToarr (Toobj (arr)); -             }; -              in              -             vararr = [1,1,2,2,3,3,4,4,5,5,5,6,6,6,6,6,6,5,4,3,54,8,11]; to              +Alert (Getuniq (arr));

Use JavaScript to remove duplicates in an array

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.