Categories
JavaScript Answers

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

Spread the love

Sometimes, we want to search an array for a substring match with JavaScript.

In this article, we’ll look at how to search an array for a substring match with JavaScript.

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

To search an array for a substring match with JavaScript, we call the filter and includes methods.

For instance, we write

const items = ["item 1", "thing", "id-3-text", "class"];
const matches = items.filter((s) => s.includes("thi"));

to call items.filter with a callback that checks if string s in items includes 'thi' with includes.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *