Two methods to remove duplicate strings in js

Source: Internet
Author: User

Method 1:
Copy codeThe Code is as follows:
Function checkRe (checkid ){

Var id = window. parent. getQry ("ctrid") + "_";

Var values = paipaiparent.opener.doc ument. getElementById (id + "txt_dept_name"). value;
Var keys = paipaiparent.opener.doc ument. 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 );

Optional bytes parent.opener.doc ument. getElementById (id + "txt_dept_name"). value = values;
Optional bytes parent.opener.doc ument. getElementById (id + "hf_ids"). value = keys;

Return;

}
// Convert to an array, remove duplicates, and then combine them.
Function okd (str ){

Var ary = str. split (",");
// Deduplication Algorithm
Var json = {};
For (var I = 0; I <ary. length; I ++ ){

If (ary [I]! = ""){

Json ["a" + ary [I] = ary [I];
}
}

// View the result
Var str2 = "";
For (var key in json ){
Str2 + = "," + json [key];
}

Var s = str2.toString ();

// Remove the first comma
If (s. substr (0, 1) = ','){
S = s. substr (1 );
}

Return s;
}

Function add_all (){
Var ck = document. getElementsByTagName ("input ");
For (I = 0; I <ck. length; I ++ ){
If (ck [I]. type = "checkbox "){
Ck [I]. checked = true;
CheckRe (ck [I]);

}
}

}

Function del_all (){
Var id = window. parent. getQry ("ctrid") + "_";
Optional bytes parent.opener.doc ument. getElementById (id + "txt_dept_name"). value = "";
Optional bytes parent.opener.doc ument. getElementById (id + "hf_ids"). value = "";

Var ck = document. getElementsByTagName ("input ");
For (I = 0; I <ck. length; I ++ ){
If (ck [I]. type = "checkbox "){
Ck [I]. checked = false;

}
}
}

Copy codeThe Code is as follows:
<Input id = 'chk _ pkid' type = 'checkbox' name = 'chk' onclick = 'checkre (this); 'value = 'value | pkid'/>

Method 2:
Copy codeThe Code is as follows:
Function filterRepeatStr (str ){
Var ar2 = str. split (",");
Var array = new Array ();
Var j = 0
For (var I = 0; I <ar2.length; I ++ ){
If (array = "" | array. toString (). match (new RegExp (ar2 [I], "g") = null) & ar2 [I]! = ""){
Array [j] = ar2 [I];
Array. sort ();
J ++;
}
}
Return array. toString ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.