How to use includes() method and filter() method In Javascript
First, we will create an array of strings, and then use includes() and filter() methods to check whether the array elements contain a sub-string or not.
Example: This example shows the implementation of the above-explained approach.
const arr = ['Geeks', 'gfg', 'w3wiki', 'abc'];
const substr = 'eks';
const subArr = arr.filter(str => str.includes(substr));
console.log(subArr);
Output
[ 'Geeks', 'w3wiki' ]
Check an array of strings contains a substring in JavaScript
Checking if an array of strings contains a substring in JavaScript involves iterating through each string in the array and examining if the substring exists within any of them. This process ensures efficient detection of the substring’s presence within the array.
These are the following ways to do this:
Table of Content
- Using includes() method and filter() method
- Using includes() method and some() method
- Using indexOf() method
- Using Lodash _.strContains() Method
- Using a for of loop
Contact Us