javascript 數(shù)組去重復(fù)(在線去重工具)
更新時間:2016年12月17日 13:24:00 投稿:mdxy-dxy
很多情況下我們需要去掉重復(fù)的內(nèi)容,一般我們都是將很多內(nèi)容放到一個數(shù)組里面,然后再去重復(fù),這里簡單為大家整理一下
第一種方法:
<textarea id="list" class="toolarea">腳本之家 chabaoo.cn www.baidu.com chabaoo.cn 腳本之家 shouce.jb51.net chabaoo.cn tools.jb51.net www.baidu.com shouce.jb51.net tools.jb51.net 腳本之家 chabaoo.cn</textarea> <input type="button" onclick="process()" value="去重處理" class="bt-green"> <script type="text/javascript"> function unique(ary) { var i = 0, gid='_'+(+new Date)+Math.random(), objs = [], hash = { 'string': {}, 'boolean': {}, 'number': {} }, p, l = ary.length, ret = []; for (; i < l; i++) { p = ary[i]; if (p == null) continue; tp = typeof p; if (tp in hash) { if (!(p in hash[tp])) { hash[tp][p] = 1; ret.push(p); } } else { if (p[gid]) continue; p[gid]=1; objs.push(p); ret.push(p); } } for(i=0,l=objs.length;i<l;i++) { p=objs[i]; p[gid]=undefined; delete p[gid]; } return ret; } function process(){ list = document.getElementById('list'); arr = list.value.split('\n'); arr = unique(arr); list.value = ""; for(key in arr){ list.value += arr[key] + '\n'; } alert("處理完成!"); } </script>
第二種方法
function sx(){ var rntArray=[],temp,hasValue; var array=document.getElementById("neirong").value.split("\n"); for(var i in array){ temp=array[i]; hasValue=false; for(var j in rntArray){ if(temp===rntArray[j]){ hasValue=true; break; } } if(hasValue===false){ rntArray.push(temp); } } document.getElementById("neirong1").value=rntArray.join("\n"); }
相關(guān)文章
JavaScript中的構(gòu)造函數(shù)和實例對象之間的關(guān)系(構(gòu)造器)
這篇文章主要介紹了JavaScript中的構(gòu)造函數(shù)和實例對象之間的關(guān)系(構(gòu)造器),需要的朋友可以參考下2023-05-05document.all與getElementById、getElementsByName、getElementsByT
Document.all[]是文檔中所有標(biāo)簽組成的一個數(shù)組變量,包括了文檔對象中所有元素2008-12-12