//数据存在于list这个输入框中,然后直接调用此函数
function process() {
var list = document.getElementById('list');
arr = list.value.split('\n');
arr = unique(arr);
list.value = "";
for (key in arr) {
list.value += arr[key] + '\n';
}
alert("处理完成!");
}
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;
}
JavaScript去除文本框中重复内容 js去重复
于 2021-08-03 16:06:19 首次发布
本文介绍了一种使用JavaScript实现的数组去重方法,并演示了如何处理数组元素。该方法适用于前端开发场景,通过自定义函数实现了数组元素的去重,并能够重新填充到指定的输入框中。

217

被折叠的 条评论
为什么被折叠?



