Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

To check the array contains a value in typescript, use the includes() method it will return the result in true or false depending upon substring contains or not.

This tutorial will help you to check value contains or presented or not in an array, here we are going to use Javascript includes() method to check whether the value include or not in an array on typescript syntax.

Javascript provides the includes() method to search value in the string but we can also use it to check value exist or not in an array or array of objects.

Let’s start today’s tutorial How to check an array contains a value in typescript?

we first try the includes() method in typescript single-dimension of arrays.

const names: Array<string> = ["Infinit", "Bility"];

names.includes("Infinit");  // true âś…
names.includes("Inf");  // false ❌

const counts: Array<number> = [1, 2];

counts.includes(1);  // true âś…
counts.includes(3);  // false ❌

Now, let’s try the includes() method in an array of objects.

interface user {
    id: number,
    name: string
}

const users: Array<user> = [{id: 1, name: "Infinit"},{id: 2, name: "Bility"}];


users.find(e => e.name.includes("Infinit"));   // { id: 1, name: 'Infinit' } âś…
users.find(e => e.name.includes("Lit"));   // undefined ❌