Search array inArray but ignore case

2019-05-23 11:11发布

问题:

Hope I provided a clear title.

I am creating an array comprised of all options, trimming whitespace, and storing. (correct me if im wrong in my process). On input change, if option array returns false, clear the next field, else, store the unique new value and place in next field.

If array contains:

blue
black
brown

and I search for BLUE or Blue or BLue...etc, the result returns false. I guess im asking for caseInsensitive logic, but not sure how I can incorporate.

Ive done this all on trial and error so please feel free to refine if your time allots. Also, this needs to be within spec of jquery 1.3

http://jsfiddle.net/arkjoseph/dRpmq/2/

thank you for assistance.

回答1:

Since it looks like you have control of the values (and are making them lowercase), you can do a pretty simple change from:

if (($.inArray((this.value), option) > 0)

to

if (($.inArray((this.value.toLowerCase()), option) > 0)

Demo at http://jsfiddle.net/dRpmq/3/



回答2:

From Case insensitive search in array JavaScript provide a Method indexOf() which is used to search object from array but this is case sensitive.

Jquery provide a method jQuery.inArray() To search object in array but this is case sensitive also i.e “a” is not equal to “A”. So we have to make our own function, Following is small javascript function which return index of object if found in array otherwise return -1 and this is case insensitive method