Copy codeThe Code is as follows:
// General implementation 1:
Function isHasElementOne (arr, value ){
For (var I = 0, vlen = arr. length; I <vlen; I ++ ){
If (arr [I] = value ){
Return I;
}
}
Return-1;
}
// Implementation 2:
Function isHasElementTwo (arr, value ){
Var str = arr. toString ();
Var index = str. indexOf (value );
If (index> = 0 ){
// An index is returned.
Var reg1 = new RegExp ("(^ |,)" + value + "(, | $)", "gi ");
Return str. replace (reg1, "$2 @ $3"). replace (/[^, @]/g, ""). indexOf ("@");
} Else {
Return-1; // This item does not exist
}
}
Supplement:
Copy codeThe Code is as follows:
Function isHasElement (arr, value ){
Var str = arr. toString ();
Var index = str. indexOf (value );
If (index> = 0 ){
// An index is returned.
// "(^" + Value + ",) | (," + value + ",) | (," + value + "$ )"
Value = value. toString (). replace (/(\ [| \])/g, "\ $1 ");
Var reg1 = new RegExp ("(^ |,)" + value + "(, | $)", "gi ");
Return str. replace (reg1, "$2 @ $3"). replace (/[^, @]/g, ""). indexOf ("@");
} Else {
Return-1; // This item does not exist
}
}
Recently, I encountered efficiency problems when writing the jquery combobox plug-in. In addition, the jquery selector class selection causes low efficiency. After method 2 is adopted, the efficiency is significantly improved.