Sometimes, we want to search an array for a substring match in JavaScript.
In this article, we’ll look at how to search an array for a substring match in JavaScript.
How to search an array for a substring match in JavaScript?
To search an array for a substring match in JavaScript, we can use the array filter method.
For instance, we write
const items = ["item 1", "thing", "id-3-text", "class"];
const matches = items.filter((s) => s.includes("thi"));
to call filter with a callback that calls the string includes method to return an array that has strings in items that has the 'thi' substring.
Conclusion
To search an array for a substring match in JavaScript, we can use the array filter method.