Sample Code for JS array deduplication and deduplication

Source: Internet
Author: User

This article mainly introduces the sample code for de-duplication and de-duplication of JS arrays. If you need it, you can refer to it for help.

Method 1: code for deduplication: <script> Array. prototype. distinct = function () {var a = [], B = []; for (var prop in this) {var d = this [prop]; if (d = a [prop]) continue; // prevents loops to prototype if (B [d]! = 1) {. push (d); B [d] = 1;} return a;} var x = ['A', 'B', 'C', 'D ', 'B', 'A', 'E', 'A', 'B', 'B', 'C', 'D', 'B', 'A ', 'E']; document. write ('original array: '+ x); document. write ("<br/>"); document. write ('after deduplication: '+ x. distinct (); </script> Method 2: the code for getting duplicate data is as follows: <script type = "text/javascript"> Array. prototype. distinct = function () {var a = [], B = [], c = [], d = []; for (var prop in this) {var d = this [prop]; if (d = a [prop]) {continue;} // prevents loops to prototype if (B [D]! = 1) {. push (d); B [d] = 1;} else {c. push (d); d [d] = 1 ;}// return a; return c. distinct1 ();} Array. prototype. distinct1 = function () {var a = [], B = []; for (var prop in this) {var d = this [prop]; if (d = a [prop]) continue; // prevents loops to prototype if (B [d]! = 1) {. push (d); B [d] = 1;} return a;} var x = ['A', 'B', 'C', 'D ', 'B', 'A', 'E', 'A', 'B', 'B', 'C', 'D', 'B', 'A', 'E ', 'F', 'F', 'G']; document. write ('original array: '+ x); document. write ("<br/>"); document. write ('after deduplication: '+ x. distinct (); </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.