首先要将文本按照/n分割成数组,然后进行数据去重操作,再重新转换成文本,代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
var testarr = ["123", "bfw", "123", "bfw"];
console.log(unique(testarr));
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
}
</script>
</head>
<body>
</body>
</html>
网友回复