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?
11 Respostas
+ 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
+ 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).
+ 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.
+ 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?
+ 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.
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.
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
0
Muthumani V
If there is 2 entry of abc then? There should be always single entry? Can you make sure?
0
🅰🅹 🅐🅝🅐🅝🅣 There should be always single entry in my scenario. Only one entry. There is no duplicate value.
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 ?
0
Okay. try using a regex with i flag.