Check if TypeScript Array Contains a String

Currently, I am using Angular 2.0 and have an array defined as follows: var channelArray: Array = [‘one’, ‘two’, ‘three’];

How can I check in TypeScript whether channelArray contains the string ‘three’? I am looking for a way to typescript array contains.

Ah, I’ve worked with Angular for a while, so I can help you out here. You can check if channelArray contains the string ‘three’ using similar methods from JavaScript.

Here’s one approach using Array.prototype.indexOf():


console.log(channelArray.indexOf('three') > -1);

This checks if ‘three’ is present by verifying that the index is greater than -1.

Alternatively, a more modern and cleaner approach is using ECMAScript 2016’s Array.prototype.includes():


console.log(channelArray.includes('three'));

This is a more straightforward method that returns true if ‘three’ is in the array. Both of these work well when you want to check if a string exists in a TypeScript array.

For more complex scenarios, like when working with objects, you might find find(), some(), or filter() more suitable. For instance:

  • Using find():

const arr = [{foo: 'bar'}, {foo: 'baz'}];

console.log(arr.find(e => e.foo === 'bar')); // {foo: 'bar'}

  • Using some():

console.log(arr.some(e => e.foo === 'bar')); // true

  • Using filter():

console.log(arr.filter(e => e.foo === 'bar')); // [{foo: 'bar'}]

These are particularly useful when dealing with arrays of objects. If you’re working with plain strings though, includes() is usually your go-to method for a quick “typescript array contains” check.

Yeah, I agree with Madhurima. The most efficient way in modern JavaScript or TypeScript to check if an array contains a string is to use the includes() method. It’s clean and easy to use. Here’s an example based on your situation:


var fruits = ["Banana", "Orange", "Apple", "Mango"];

var containsMango = fruits.includes("Mango");

console.log(containsMango); // true

The includes() method checks if an array contains a specified element and returns true if it’s present. If not, it returns false. This method is perfect when you’re working with an array of strings and want a simple “typescript array contains” check.

One thing to note: includes() is case-sensitive. So if you’re checking for “mango” instead of “Mango”, it would return false.

For arrays of objects, using find(), some(), or filter() (as Madhurima pointed out) is better when dealing with more complex data structures.

Absolutely! Just to add on to what Sam and Madhurima said, if you’re working with more complex data or if you want to extract multiple elements that meet certain criteria, the filter() method is fantastic. It allows you to get all the matches in one go. Here’s how you might use it:


this.products = array_products.filter((x) => x.Name.includes("ABC"));

In this example, filter() creates a new array with only those elements where the Name property contains the substring “ABC”. This method is great for larger datasets or when you need more than just a single result.

For basic checks on strings, includes() is still the best way to perform a “typescript array contains” check. But when you need something more powerful or flexible, filter() is a great tool to keep in mind.

And like Sam mentioned, don’t forget that includes() is case-sensitive!