English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Esempio di metodo di rimozione dei duplicati degli array implementato in JS

Questo articolo spiega come implementare la rimozione dei duplicati degli array in JavaScript. Condivido con tutti per riferimento, come segue:

Esempio di esecuzione di seguito:

Esempio di codice completo di seguito:

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript" language="javascript" >
Array.prototype.distinct = function(){
 var $ = this;
 var o1 = {}; //存放去重复值
 var o2 = {}; //存放重复值
 var o3 = []; //存放重复值
 var o; //数组单个变量
 for(var i=0;o = $[i];i++){
 if(o in o1){
  if(!(o in o2)) o2[o] = o;
  delete $[i];
 }
  o1[o] = o;
 }
 }
 $.length = 0; //清空原数组
 for(o in o1){
 $.push(o);
 }
 for(o in o2){
 o3.push(o);
 }
 return o3;
}
var a = [2,2,2,3,3,3,4,4,5,6,7,7];
console.log("Original array: " + a); //2,2,2,3,3,3,4,4,5,6,7,7
console.log("The repeated elements are: " + a.distinct()); //2,3,4,7
console.log("The sorted array is: " + a);      //2,3,4,5,6,7
console.log("The length after sorting is:" + a.length)    //6
</script>
</head>
<body>
</body>
</html>

PS: Here are several duplicate removal tools provided for your reference and use:

Online Duplicate Item Removal Tool:
http://tools.jb51.net/code/quchong

Online Text Duplicate Removal Tool:
http://tools.jb51.net/aideddesign/txt_quchong

Readers who are interested in more content related to JavaScript can check the special topic of this site: 'Summary of JavaScript Array Operation Skills', 'Summary of JavaScript Sorting Algorithms', 'Summary of JavaScript Mathematical Operation Usage', 'Summary of JavaScript Data Structures and Algorithm Skills', 'Summary of JavaScript Traversal Algorithms and Skills', 'Summary of JavaScript Search Algorithm Skills', and 'Summary of JavaScript Error and Debugging Skills'.

Hoping that the content described in this article will be helpful to everyone's JavaScript program design.

Declaration: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, does not undergo manual editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @) for reporting, and provide relevant evidence. Once confirmed, this site will immediately delete the content suspected of infringement.

Ti potrebbe interessare