How to efficiently remove repeated items in the js array _ javascript skills

Source: Internet
Author: User
Tags array sort
How to efficiently remove duplicate entries in the js array? The following small series will bring you an efficient implementation method to remove repeated items in the js array. Hope to help you. Let's take a look at it with xiaobian. Method 1: Standard Mode

1. construct a new temporary array to store the results

2. In the for loop, an element is extracted from the original array each time, and compared with the temporary Array Using this element Loop

3. If this element is not in the temporary array, it is saved to the temporary array.

Method 2: The default Js array sort is used for sorting by ASCII;

To sort data in ascending or descending order: <Console print output>

1. Sort the current array first

2. Check whether the I-th element in the current is the same as the last element in the temporary array. Because it has been sorted, the repeating element will be in the adjacent position.

3. If the elements are different, the elements are stored in the result array.

Method 3: <Recommendation> Whether the json object has a property value

1. Create a new array to store the results

2. Create an empty object json

3. during a for loop, each time an element is retrieved and compared with an object, if the element is not repeated, it is stored in the result array, and the content of this element is used as an attribute of the object, and assign a value of 1 to the object created in step 1.

Note: For comparison, an element is extracted from the original array each time and accessed in the object. If the attribute can be accessed, it is repeated.

In this article, how to efficiently remove the repeated items in the js array is all the content shared by the editor. I hope to give you a reference and support for the script.

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.