Get index of a string present in an array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Get index of a string present in an array

How can we check if a string is present in an array ? The strings present in the array and the string searching for can be case insensitive and we should do this without converting strings to upper/lower case. Is there any other way using JavaScript?

5th Jun 2021, 1:05 PM
Muthumani V
Muthumani V - avatar
11 Answers
+ 3
Muthumani V Use indexOf function to get index arr = ["abc", "Abc", "ghi"] console.log(arr.indexOf("abc")) // 0 console.log(arr.indexOf("Abc")) // 1
5th Jun 2021, 1:13 PM
A͢J
A͢J - avatar
+ 3
This gets tricky cause you want case insensitive string comparison ability. I found this from web search, it's about case insensitive string comparison. But I think this will only help a little (or maybe not at all), cause you'd most likely have to compare each array element with the search term, should you try to implement either one of the 3 methods described in the tutorial 👇 https://www.wikitechy.com/tutorials/javascript/javascript-case-insensitive-string-comparison One thing to note, case insensitive comparison is far less performant compared to the case sensitive comparison. So I guess it will be far slower compared to what AJ suggested (using built-in indexOf() method).
5th Jun 2021, 2:07 PM
Ipang
+ 2
Muthumani V That's not possible without using those methods because ASCII value of 'A' and 'a' are different so if you want to make it possible then you have to make same ASCII value of 'A' and 'a'. It is only possible if you use toLowerCase or toUpperCase methods.
5th Jun 2021, 4:38 PM
A͢J
A͢J - avatar
+ 1
Muthumani V Ok then what about Aravind Shetty solution? And what about this solution? arr = ["Querty", "abc", "xyz"]; x = "ABc"; for (var i = 0; i < arr.length; i++) { if (arr[i].toLowerCase() === x.toLowerCase()) { console.log(i); break; } } Without converting uppercase and lowercase is not possible?
5th Jun 2021, 4:05 PM
A͢J
A͢J - avatar
+ 1
Aravind Shetty Thanks for your code. I'm checking if it can be done in a different way. That is without using toLowerCase/upper case. My student asked me this question and I'm not sure if we can achieve it without using these methods.
5th Jun 2021, 4:18 PM
Muthumani V
Muthumani V - avatar
0
var y = 'string' var index: var flag = arr.some( (x, i)=>{ if(x.toLowerCase() == y.toLowerCase()) { index = i; return true: } }) flag becomes true if the string matches ( case insensitive ) and We will get the index where the match happened. And doesn't change any value inside the array.
5th Jun 2021, 2:07 PM
Aravind Shetty
Aravind Shetty - avatar
0
🅰🅹 🅐🅝🅐🅝🅣 Thanks for your answer. I don't want 2 entries for abc. Say Array consist of 3 values arr = ["qwerty","AbC","ghi"]; Input: aBc Output : 1 Input: ABC Output : 1 Note: I want to get index without converting array or searched string to upper/lower case
5th Jun 2021, 3:32 PM
Muthumani V
Muthumani V - avatar
0
Muthumani V If there is 2 entry of abc then? There should be always single entry? Can you make sure?
5th Jun 2021, 3:58 PM
A͢J
A͢J - avatar
0
🅰🅹 🅐🅝🅐🅝🅣 There should be always single entry in my scenario. Only one entry. There is no duplicate value.
5th Jun 2021, 4:01 PM
Muthumani V
Muthumani V - avatar
0
May be you have a misconception.. above code that I have written doesn't change your variable. The string value is intact and you can access again using 'y' variable.. The toLowerCase method just return a new string. Your input and array doesn't change. Isn't that what you want?. why do you not want to use toLowerCase method ? can you share your code ?
5th Jun 2021, 4:06 PM
Aravind Shetty
Aravind Shetty - avatar
0
Okay. try using a regex with i flag.
5th Jun 2021, 4:32 PM
Aravind Shetty
Aravind Shetty - avatar