// 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) {// index var reg1 = new Regexp ("(^ |,)" + value + "(, | $) "," Gi "); Return Str. replace (reg1, "$2 @ $3 "). replace (/[^, @]/g ,""). indexof ("@");} else {return-1; // This item does not exist }}
Supplement:
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.