JQuery标签去重与数组去重

时间:2023-01-30 08:22:56

如图所示

JQuery标签去重与数组去重

代码

 

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <title></title>
 6     <script src="../js/jquery-1.10.2.js"></script>
 7     <script type="text/javascript">
 8         $(document).ready(function () {
 9 
10             //标签去重
11             $.each($(".drop"), function (key, val_1) {
12                 var count = 0;
13                 $.each($(".drop"), function (key, val_2) {
14                     if ($(val_1).text() == $(val_2).text()) {
15                         count = count + 1;
16                         if (count >= 2) {
17                             $(this).remove();
18                         }
19                     }
20                 });
21             });
22 
23             //数组去重
24             var arr = new Array(2009, 2009, 2010, 2010, 2009, 2010);
25             arr.sort();//排序
26             jQuery.unique(arr);//去重
27             $.each(arr, function (key, val) {
28                 console.log('index in arr:' + key + ", corresponding value:" + val);
29             });
30         });
31     </script>
32 </head>
33 <body>
34     <div>标签去重</div>
35     <div>
36         <span class="drop">张三</span>
37         <span class="drop">李四</span>
38         <span class="drop">王五</span>
39         <span class="drop">张三</span>
40         <span class="drop">王五</span>
41     </div>
42     
43 </body>
44 </html>