JavaScript

Filter Strings in Array Based on Content (Filter Search Value)

In JavaScript programming, filtering strings in an array depending on their content is a typical task. It is commonly used when you want to search through a collection of strings and extract/retrieve only the ones that match a specified search value. For this, JavaScript provides a “filter()” method with the collaboration of some other built-in methods, such as the “includes()” method.

This tutorial will define the methods to filter strings in an array based on content.

How to Filter Strings in Array Based on Content?

To filter strings in an array based on content, use the following methods:

Method 1: Filter Strings in Array Based on Content Using “filter()” Method With “indexOf()” Method

Use the “filter()” method with the “indexOf()” method for filtering strings in an array based on content. The “filter()” method gives a new array containing elements that satisfy the given test implemented by the callback function. “indexOf()” tells the position of an element if it exists. If the given value is not found, it gives “-1”. It outputs the location or index of the first occurrence/instance of a particular value. When the string in an array matches the beginning of the targeted/searched string, it gives “0”.

Syntax

Use the given syntax for indexOf() method:

indexOf(searchedString)

Example

Create an array of languages named “array”:

var array = ["Java", "JavaScript", "Node.js", "Angular.js"];

Create a variable “content” that stores a search string “Java”:

var content = 'Java';

Call the “filter()” method with the “indexOf()” method to filter the strings from an array that matches the string “Java”:

var filtered = array.filter(function (str) {
  return str.indexOf(content) === 0;
});

Finally, print the resultant array of strings that matches the searched string:

console.log(filtered);

Output

Method 2: Filter Strings in Array Based on Content Using “filter()” Method With “test()” Method

To filter strings in an array, you can use the “filter()” method with the “test()” method. “test()” method is a regular expression method that searches a string for a given pattern and gives a boolean value that indicates whether the pattern was found or not. It outputs “true” if the given pattern is matched/found otherwise, it returns “false”.

Syntax

Follow the given syntax for the “test()” method:

pattern.test(string)

Example

Store the searched string as a regular expression or regex in a variable “content”:

var content = /js/;

Invoke the “filter()” method with the “test()” method to filter the strings from an array that matches the string “js”:

var filtered = array.filter(function (str) {
  return content.test(str);
});

Lastly, print the matched strings in an array on the console:

console.log(filtered);

The output displays the strings that contain “js”:

Method 3: Filter Strings in Array Based on Content Using “filter()” Method With “includes()” Method

Another way to filter strings in an array based on contents is the “includes()” method with the “filter()” method. The includes() method within the filter() method can verify whether an array includes a string with a searched string/value, and include it in the new array only if it does. The includes() method gives the “true” or “false” based on the existence of the element.

Syntax

The following syntax is utilized for the includes() method:

includes(searchedString)

Example

Store the string “Script” in variable “content” as a searched string:

var content = 'Script';

Verify whether the array contains a string with the searched string using the “includes()” method with the “filter()” method:

var filtered = array.filter(function (str) {
  return str.includes(content);
});

Now, display the resultant array of strings:

console.log(filtered);

The output indicates that there is a string in an array that contains the content “String”:


We have provided all the necessary instructions relevant to the filtering strings in an array based on contents in JavaScript.

Conclusion

To filter strings in an array based on content, use the “filter()” method with the “indexOf()” method, “test()” method, or “includes()” method. “indexOf()” method is used for locating the position or index of the elements. The “test()” method and “includes()” method verifies whether the specified searched value or string is included in the string/array. This tutorial demonstrated the methods to filter strings in an array based on content.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.