js去除重复字符串两种实现方法_javascript技巧
程序员文章站
2022-04-14 15:25:37
...
方法一:
function checkRe(checkid) {
var id = window.parent.getQry("ctrid") + "_";
var values = window.parent.opener.document.getElementById(id + "txt_dept_name").value;
var keys = window.parent.opener.document.getElementById(id + "hf_ids").value;
var ary = checkid.value.split("|");
var newsValues = ary[0];
var newsKeys = ary[1];
if (checkid.checked) {
values = values + "," + newsValues;
keys = keys + "," + newsKeys;
}
else {
values = values.replace(new RegExp("," + newsValues), "");
keys = values.replace(new RegExp("," + newsKeys), "");
}
values = okd(values);
keys = okd(keys);
window.parent.opener.document.getElementById(id + "txt_dept_name").value = values;
window.parent.opener.document.getElementById(id + "hf_ids").value = keys;
return;
}
//转换成数组,去掉重复,再组合好。
function okd(str) {
var ary = str.split(",");
// 去重的算法
var json = {};
for (var i = 0; i
if (ary[i] != "") {
json["a" + ary[i]] = ary[i];
}
}
// 查看结果
var str2 = "";
for (var key in json) {
str2 += "," + json[key];
}
var s = str2.toString();
//去掉第一个逗号
if (s.substr(0, 1) == ',') {
s = s.substr(1);
}
return s;
}
function add_all() {
var ck = document.getElementsByTagName("input");
for (i = 0; i if (ck[i].type == "checkbox") {
ck[i].checked = true;
checkRe(ck[i]);
}
}
}
function del_all() {
var id = window.parent.getQry("ctrid") + "_";
window.parent.opener.document.getElementById(id + "txt_dept_name").value = "";
window.parent.opener.document.getElementById(id + "hf_ids").value = "";
var ck = document.getElementsByTagName("input");
for (i = 0; i if (ck[i].type == "checkbox") {
ck[i].checked = false;
}
}
}
方法二:
function filterRepeatStr(str){
var ar2 = str.split(",");
var array = new Array();
var j=0
for(var i=0;iif((array == "" || array.toString().match(new RegExp(ar2[i],"g")) == null)&&ar2[i]!=""){
array[j] =ar2[i];
array.sort();
j++;
}
}
return array.toString();
}
复制代码 代码如下:
function checkRe(checkid) {
var id = window.parent.getQry("ctrid") + "_";
var values = window.parent.opener.document.getElementById(id + "txt_dept_name").value;
var keys = window.parent.opener.document.getElementById(id + "hf_ids").value;
var ary = checkid.value.split("|");
var newsValues = ary[0];
var newsKeys = ary[1];
if (checkid.checked) {
values = values + "," + newsValues;
keys = keys + "," + newsKeys;
}
else {
values = values.replace(new RegExp("," + newsValues), "");
keys = values.replace(new RegExp("," + newsKeys), "");
}
values = okd(values);
keys = okd(keys);
window.parent.opener.document.getElementById(id + "txt_dept_name").value = values;
window.parent.opener.document.getElementById(id + "hf_ids").value = keys;
return;
}
//转换成数组,去掉重复,再组合好。
function okd(str) {
var ary = str.split(",");
// 去重的算法
var json = {};
for (var i = 0; i
if (ary[i] != "") {
json["a" + ary[i]] = ary[i];
}
}
// 查看结果
var str2 = "";
for (var key in json) {
str2 += "," + json[key];
}
var s = str2.toString();
//去掉第一个逗号
if (s.substr(0, 1) == ',') {
s = s.substr(1);
}
return s;
}
function add_all() {
var ck = document.getElementsByTagName("input");
for (i = 0; i if (ck[i].type == "checkbox") {
ck[i].checked = true;
checkRe(ck[i]);
}
}
}
function del_all() {
var id = window.parent.getQry("ctrid") + "_";
window.parent.opener.document.getElementById(id + "txt_dept_name").value = "";
window.parent.opener.document.getElementById(id + "hf_ids").value = "";
var ck = document.getElementsByTagName("input");
for (i = 0; i if (ck[i].type == "checkbox") {
ck[i].checked = false;
}
}
}
复制代码 代码如下:
方法二:
复制代码 代码如下:
function filterRepeatStr(str){
var ar2 = str.split(",");
var array = new Array();
var j=0
for(var i=0;i
array[j] =ar2[i];
array.sort();
j++;
}
}
return array.toString();
}
推荐阅读
-
mysql优化小技巧之去除重复项实现方法分析【百万级数据】
-
JS实现字符串中去除指定子字符串方法分析
-
JavaScript trim 实现去除字符串首尾指定字符的简单方法
-
JS实现去除数组中重复json的方法示例
-
两种方法实现在HTML页面加载完毕后运行某个js_javascript技巧
-
js实现防止被iframe的方法_javascript技巧
-
js 未结束的字符串常量错误解决方法_javascript技巧
-
js中判断对象是否为空的三种实现方法_javascript技巧
-
js中判断对象是否为空的三种实现方法_javascript技巧
-
js判断为空Null与字符串为空简写方法_javascript技巧