Javascript array deduplication Method Analysis

Source: Internet
Author: User
Tags javascript array
This article mainly introduces the javascript array deduplication method, and compares and analyzes the principle and Operation Skills of javascript Array deduplication through two examples, for more information about how to deduplicate javascript arrays, see the example in this article. We will share this with you for your reference. The details are as follows:

Method 1.

Idea: Create a New empty array, traverse the old array cyclically, and use the indexOf () method to obtain the position of the element in the array. If the value is-1, it indicates that the element does not exist. Then, the new array uses indexOf to obtain each element of the old array. If the value is-1, it indicates that the element does not exist. Then, it pushes the element to the new array and outputs the new array, that is, the de-duplicated array.

Var arr = [24, 56,]; var new_arr = []; for (var I = 0; I

Method 2.

Var arr = [, 49];
Var o = {}; var new_arr = []; for (var I = 0; I

The same principle is to use an empty object to determine

Note: The second method is to store existing objects into an object in the form of a subscript reference, which is much faster than searching arrays with indexOf, therefore, the first method consumes resources.

I hope this article will help you design JavaScript programs.

For more articles on de-duplicated javascript array analysis, refer to PHP Chinese network!

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.