Navigate back to the homepage

How to check array contains value in typescript?

Infinitbility
TypeScript
November 6th, 2021 · 1 min read
How to check array contains value in typescript?

Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to check value contains or present or not in an array, here we are going to use Javascript includes() method to check 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 start today tutorial How to check array contains a value in typescript?

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

1const names: Array<String> = ["Infinit", "Bility"];
2
3names.includes("Infinit"); // true ✅
4names.includes("Inf"); // false ❌
5
6const counts: Array<Numbers> = [1, 2];
7
8counts.includes(1); // true ✅
9counts.includes(3); // false ❌

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

1const users: Array<Object> = [{id: 1, name: "Infinit"},{id: 2, name: "Bility"}];
2
3
4users.find(e => e.name.includes("Infinit")); // { id: 1, name: 'Infinit' } ✅
5users.find(e => e.name.includes("Lit")); // undefined ❌

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to check if index exists in array javascript?

How to check if index exists in array javascript?

Example of check index exist in array in javascript, React, React Native, vue, node, deno, and all javascript framework.

November 5th, 2021 · 1 min read
How to check value exist in array of object in javascript?

How to check value exist in array of object in javascript?

Example of check value exist in array of object in javascript, React, React Native, vue, node, deno, and all javascript framework.

November 4th, 2021 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]