Check if array of strings contains a substring in JavaScript

Check if Array of Strings contains a Substring in JavaScript

We will learn how to check if array of strings contains a substring in JavaScript with two solutions: using filter() and find().

How to check if array of strings contains a substring in JavaScript

Using filter()

To check if array of strings contains a substring in JavaScript, you should get all the strings in the array that contains the specific substring and then print out these elements. The syntax and usage of the filter() method can be found here.

Firstly, let’s see what this method will return:

let arr = ["learnshareit", "LearnShare IT", "LearnShareIT"];
let substr = "IT";

// Get the strings in the array that contains the substring "IT"
let strArray = arr.filter((v) => v.includes(substr));
console.log(strArray);

Output: 

['LearnShare IT', 'LearnShareIT']

The above example defined a new array to use the filter() function. We also use the includes() method on a string to check if the substring is contained in the string or not. The filter will return a new array of strings that contains the substring “IT”. However, if you don’t want to receive an array, but you want to receive true or false, then you can print out whether that array has a length larger than 0 or not:

let arr = ["learnshareit", "LearnShare IT", "LearnShareIT"];
let substr = "IT";

// Check if array of strings contains a substring "IT"
let strArray = arr.filter((v) => v.includes(substr));
console.log(strArray.length > 0);

Output: 

true

As you can see, the output is true, which means that your array contains at least one substring you have declared. Next, we will show you a better optimized method to do this.

Using find()

This method will return a first found element in the array that satisfies the condition.

Syntax:

find(function)

Parameter:

  • function: the checking function, which returns a boolean

The find() method will stop iterating through the elements in the array whenever it finds one element that matches the condition of the checking function:

let arr = ["learnshareit", "LearnShare IT", "LearnShareIT"];
let substr = "IT";

// Check if array of strings contains a substring "IT"
let res = arr.find((v) => v.includes(substr));
console.log(res);

Output: 

Learn Share IT

As can be seen, this method only returns the first string in the array that contains a substring. This will help you reduce a lot of time because you only need to check if there is any string in the array containing a substring or not. If you want to get a boolean value instead, you can check if the result returned with the Boolean() constructor:

let arr = ["learnshareit", "LearnShare IT", "LearnShareIT"];
let substr = "IT";

// Check if array of strings contains a substring "IT"
let res = arr.find((v) => v.includes(substr));
console.log(Boolean(res));

Output: 

true

Summary

We have discovered how to check if array of strings contains a substring in JavaScript. We recommend you use the find() method as it is the quickest; If you have any questions, please leave us a reply, and we will answer soon. Have a nice day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *