Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  195] [ 1]  / answers: 1 / hits: 33677  / 10 Years ago, wed, november 19, 2014, 12:00:00

I decided to use javascript indexOf to determine if a value exists in a list / array. This was working for me untill I realised that I need to find an exact match in the list.



Here is my example code:



if (['eu', 'fr-CA', 'lt', 'sv', 'zh-CN', 'zh-TW'].indexOf('fr') > -1) {


The above code will 'match' fr to fr-CA, which is a problem for me, as I must have an exact match to fr and not match to fr-CA.



I thought that this would be straight forward, but I have searched SO and google, and cannot find an answer to getting the exact match.


More From » jquery

 Answers
0

Thats wrong, you can use indexOf:




arr.indexOf(searchElement[, fromIndex = 0])



indexOf compares searchElement to elements of the Array using strict
equality (the same method used by the ===, or triple-equals, operator)




So ['eu', 'fr-CA', 'lt', 'sv', 'zh-CN', 'zh-TW'].indexOf('fr') returns -1.


[#68769] Saturday, November 15, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alfredoc

Total Points: 261
Total Questions: 128
Total Answers: 89

Location: French Polynesia
Member since Sun, Aug 2, 2020
4 Years ago
;