Check if an Array contains a Value in TypeScript

avatar

Borislav Hadzhiev

Last updated: Feb 15, 2022

banner

Photo from Unsplash

Check if an Array contains a Value in TypeScript #

Use the includes() method to check if an array contains a value in TypeScript, e.g. if (arr.includes('two')) {}. The includes method will return true if the value is contained in the array and false otherwise.

index.ts
const arr: string[] = ['one', 'two', 'three']; if (arr.includes('two')) { console.log('✅ two is contained in array'); } else { console.log('⛔️ two is NOT contained in array'); }

The Array.includes method performs a case-sensitive check whether a value is contained in an array.

To perform a case insensitive check whether a string is contained in an array:

  1. Pass a function to the Array.find() method.
  2. Lowercase the array element and the string and do an equality check.
  3. The Array.find method returns the first array element that satisfies the condition.
index.ts
const arr: string[] = ['one', 'two', 'three']; const str = 'TWO'; const found = arr.find((element) => { return element.toLowerCase() === str.toLowerCase(); }); console.log(found); // 👉️ "two" if (found !== undefined) { console.log('✅ the string is contained in the array'); } else { console.log('⛔️ the string is NOT contained in the array'); }

The function we passed to the Array.find method gets invoked with each element in the array until it returns a truthy value or iterates over all elements.

In the code example, the case insensitive check for the string succeeded and Array.find returned the corresponding array element.

If all of the invocations of the function we passed to Array.find return a falsy value, then the method returns undefined.

You could also use the Array.find method to check if an object is contained in an array.

To check if a TypeScript array contains an object:

  1. Pass a function to the Array.find() method.
  2. Check whether the identifier of the object is equal to a specific value and return true if it is
  3. Array.find will return the object if the conditional check is satisfied at least once
index.ts
const arr: { id: number; name: string }[] = [ { id: 1, name: 'Tom' }, { id: 2, name: 'Alfred' }, { id: 3, name: 'Fred' }, ]; const found = arr.find((obj) => { return obj.id === 2; }); console.log(found); // 👉️ {id: 1, name: 'Alfred'} if (found !== undefined) { console.log('✅ the object is contained in the array'); } else { console.log('⛔️ the object is NOT contained in the array'); }
The function we passed to the Array.find method gets called with each object in the array until it returns a truthy value or iterates over all the elements.

On each iteration, we check if the object's id is equal to 2 and if it is, the condition is met and the object is returned from the find method.

An alternative approach is to use the Array.some method.

To check if a TypeScript array contains an object:

  1. Pass a function to the Array.some() method.
  2. Check if the identifier of the object is equal to a specific value and return true if it is.
  3. Array.some will return true is the conditional check is satisfied at least once.
index.ts
const arr: { id: number; name: string }[] = [ { id: 1, name: 'Tom' }, { id: 2, name: 'Alfred' }, { id: 3, name: 'Fred' }, ]; const result = arr.some((obj) => { return obj.id === 2; }); console.log(result); // 👉️ true if (result) { console.log('✅ the object is contained in the array'); } else { console.log('⛔️ the object is NOT contained in the array'); }

The Array.some() method returns a boolean result - true if the condition is satisfied at least once and false otherwise.

This might be the more intuitive approach if you don't need to get the value of the object.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.