Sometimes, we want to make array indexOf() case insensitive with JavaScript.
In this article, we’ll look at how to make array indexOf() case insensitive with JavaScript.
How to make array indexOf() case insensitive with JavaScript?
To make array indexOf() case insensitive with JavaScript, we can use the array findIndex
method.
For instance, we write
const array = ["I", "hAve", "theSe", "ITEMs"];
const index = array.findIndex((item) => q.toLowerCase() === item.toLowerCase());
to call array.findIndex
with a callback that converts the item
in array
to lower case with toLowerCase
.
And we compare it with value q
after converting it to lower case.
Then the index of the array
item that matches q
in a case insensitive manner is returned.
Conclusion
To make array indexOf() case insensitive with JavaScript, we can use the array findIndex
method.