Categories
JavaScript Answers

How to search an array for a substring match in JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.