Languages
[Edit]
EN

JavaScript - search array for substring match

0 points
Created by:
GamerGoals22
364

In this article, we would like to show you how to search an array for substring match in JavaScript.

1. Using filter() with includes() method

Edit

In this example, we use filter() and includes() methods to search for elements inside the array that contain 'id' substring. 

 

2. Using filter() with test() method

Edit

In this section, as the argument of filter() we use RegExp test() method to match the elements that start with given pattern - in this case id.

Note:

^ in regular expressions matches the beginning of a string.

 

References

Edit
  1. Array.prototype.filter() - JavaScript | MDN
  2. RegExp.prototype.test() - JavaScript | MDN
  3. Array.prototype.includes() - JavaScript | MDN

Alternative titles

  1. JavaScript - filter array to search for substring match
  2. JavaScript - filter strings in array based on content (filter search value)
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join