javascript 数组去重复(在线去重工具)
程序员文章站
2023-11-06 11:05:22
第一种方法:
第一种方法:
<textarea id="list" class="toolarea"> www.jb51.net www.baidu.com www.jb51.net shouce.jb51.net www.jb51.net tools.jb51.net www.baidu.com shouce.jb51.net tools.jb51.net www.jb51.net</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"); }