Do A Case Insensitive Search For A String In A JavaScript Array

If you want to do a case insensitive search for a string in a JavaScript array, you can use any of the following methods –

  1. Use Array.some() to find if there is atleast one string in the array which equals (===) the given string after converting both to lower case.
  2. Use Array.findIndex() to get the index of the first matching element in the array if there is one. If a matching element is not found, it returns -1.
  3. Use Array.find() to get the first matching element in the array if there is one.

Let’s discuss each of these methods in detail below.

Case Insensitive Search For A String In A JavaScript Array

do a case insensitive search for a string in a javascript array

Array.some() For Case Insensitive Search For A String In A JavaScript Array

The Array.some() method checks if at-least one element of the given array passes the test implemented by the callback function.

If it finds even one matching element, it returns true, else it returns false.

We can use this to do a case-insensitive search of a string in an array of strings as shown below.

var arr = ["JavaScript", "Ruby", "Python"];

var str = "python";

console.log(arr.some(elem => elem.toLowerCase() === str.toLowerCase())); // true

In the above code, we have an array arr of strings. We have a string str which we want to check if it is present in the array case-insensitively.

To do this, we pass a callback function to some() which converts both the array element and the string to lowercase and then checks if they are equal using ===.

If a matching array element is found, some() will return true, else it returns false.

Array.findIndex() For Case Insensitive Search For A String In A JavaScript Array

The Array.findIndex() method returns the index of the first element in the array which passes the test implemented by the callback function.

If no match is found, it returns -1.

We can use this to do a case-insensitive search for a string in an array as shown below.

var arr = ["JavaScript", "Ruby", "Python"];

var str = "python";

console.log(arr.findIndex(elem => elem.toLowerCase() === str.toLowerCase())); // 2

In the above code, we have an array arr of strings and a string str which we want to check if it is present in the array case-insensitively.

To do this, we pass a callback function to findIndex() which converts both the array element and the string to lowercase and then checks if they are equal using ===.

If a matching element is found, findIndex() will return its index, else it returns -1.

Array.find() For Case Insensitive Search For A String In A JavaScript Array

The Array.find() method returns the value of the first element in the array which passes the test implemented by the callback function.

If no match is found, it returns undefined.

We can use this to do a case-insensitive search for a string in an array as shown below.

var arr = ["JavaScript", "Ruby", "Python"];

var str = "python";

console.log(arr.find(elem => elem.toLowerCase() === str.toLowerCase())); // Python

In the above code, we have an array arr of strings and a string str which we want to check if it is present in the array case-insensitively.

To do this, we pass a callback function to find() which converts both the array element and the string to lowercase and then checks if they are equal using ===.

If a matching element is found, find() will return its value, else it returns undefined.

So, these are three ways in which you can do a case-insensitive search of a string in an array in JavaScript. Try them out and see which one works best for you.

Leave a Reply