Home >Web Front-end >JS Tutorial >Sample code for JS array deduplication and deduplication_javascript skills

Sample code for JS array deduplication and deduplication_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:02:551273browse
Method 1: Remove duplicate data
Copy the code The code is as follows:

<script><br>Array.prototype.distinct=function(){<br>var a=[],b=[];<br>for(var prop in this){<br> var d = this[prop];<br> if (d===a[prop]) continue; //Prevent looping to prototype<br> if (b[d]!=1){<br> a. push(d);<br> b[d]=1;<br> }<br>}<br>return a;<br>}<br>var x=['a','b','c ','d','b','a','e','a','b','c','d','b','a','e'];<br> document.write('Original array:' x);<br>document.write("<br />");<br>document.write('After deduplication:' x.distinct());<br></script>

Method 2: Get duplicate data
Copy Code The code is as follows:


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn