Filter JavaScript Array by Multiple Strings



Filtering an array by multiple strings in JavaScript involves identifying elements that match any string from a given list. This is commonly used in search filters, dynamic matching, or data processing tasks. JavaScript provides simple tools like the filter method and techniques such as includes for exact matches or regular expressions for pattern-based matching. These approaches help efficiently narrow down arrays based on specific criteria.

Approaches to filter array by multiple strings

Here are the following approaches to filter an array by multiple strings in JavaScript using regex (case-sensitive) and filter with includes:

To filter an array by multiple strings, use a for loop along with indexOf(). Following is the code ?

Using Regular Expressions (Case-Sensitive)

The join("|") method creates a regex pattern to match any filter string, making it ideal for dynamic or flexible matching. By default, regex is case-sensitive but can be made case-insensitive by adding the i flag (e.g., new RegExp(filters.join("|"), "i")).
Combine the strings to filter into a regular expression and test each element for matches.

Example

const array = ["apple", "banana", "cherry", "date"];
const filters = ["apple", "date"];
const regex = new RegExp(filters.join("|")); // Create regex from filters
const result = array.filter(item => regex.test(item));
console.log(result); 

Output

["apple", "date"]

Using filter with includes

The includes method checks if an array contains a specific element, making it ideal for simple, exact matches without transformations. It is inherently case-sensitive, so it distinguishes between uppercase and lowercase characters.
Directly check if each array element matches any string in the filter list.

Example

const array = ["apple", "banana", "cherry", "date"];
const filters = ["apple", "date"];
const result = array.filter(item => filters.includes(item));
console.log(result);

Output

["apple", "date"]

Using indexof() method

The indexOf() method in JavaScript searches for a specified substring within a string. In the above program, details[index].indexOf(matchWords[outer]) checks if the word from matchWords exists in the current string from the details array. If the word is found, indexOf() returns the index of the first occurrence; otherwise, it returns -1.

Example

var details = [
   'My first Name is John and last Name is Smith',
   'My first Name is John and last Name is Doe',
   'Student first Name is John and last Name is Taylor'
];
var isPresent;
var records = [];
var matchWords = ['John', 'Doe'];
for (var index = 0; index < details.length; index++){
   isPresent = true;
   for (var outer = 0; outer< matchWords.length; outer++) {
      if (details[index].indexOf(matchWords[outer]) === -1) {
         isPresent = false;
         break;
      }
   }
   if (isPresent){
      records.push(details[index]);
   }
}
console.log(records)

To run the above program, you need to use the following command ?

node fileName.js.

Here, my file name is demo151.js.

Output

This will produce the following output ?

PS C:\Users\Amit\JavaScript-code> node demo151.js
[ 'My first Name is John and last Name is Doe'
Updated on: 2024-11-29T19:11:57+05:30

837 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements