How do I check whether an array contains a string in TypeScript?

JavascriptArraysTypescript

Javascript Problem Overview


Currently I am using Angular 2.0. I have an array as follows:

var channelArray: Array<string> = ['one', 'two', 'three'];

How can I check in TypeScript whether the channelArray contains a string 'three'?

Javascript Solutions


Solution 1 - Javascript

The same as in JavaScript, using Array.prototype.indexOf():

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

Or using ECMAScript 2016 Array.prototype.includes():

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

Note that you could also use methods like showed by @Nitzan to find a string. However you wouldn't usually do that for a string array, but rather for an array of objects. There those methods were more sensible. For example

const arr = [{foo: 'bar'}, {foo: 'bar'}, {foo: 'baz'}];
console.log(arr.find(e => e.foo === 'bar')); // {foo: 'bar'} (first match)
console.log(arr.some(e => e.foo === 'bar')); // true
console.log(arr.filter(e => e.foo === 'bar')); // [{foo: 'bar'}, {foo: 'bar'}]

Reference

Array.find()

Array.some()

Array.filter()

Solution 2 - Javascript

You can use the some method:

console.log(channelArray.some(x => x === "three")); // true

You can use the find method:

console.log(channelArray.find(x => x === "three")); // three

Or you can use the indexOf method:

console.log(channelArray.indexOf("three")); // 2

Solution 3 - Javascript

If your code is ES7 based (or upper versions):

channelArray.includes('three'); //will return true or false

If not, for example you are using IE with no babel transpile:

channelArray.indexOf('three') !== -1; //will return true or false

the indexOf method will return the position the element has into the array, because of that we use !== different from -1 if the needle is found at the first position.

Solution 4 - Javascript

Also note that "in" keyword does not work on arrays. It works on objects only.

propName in myObject

Array inclusion test is

myArray.includes('three');

Solution 5 - Javascript

Use JavaScript Array includes() Method

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var n = fruits.includes("Mango");

Try it Yourself » link

Definition

The includes() method determines whether an array contains a specified element.

This method returns true if the array contains the element, and false if not.

Solution 6 - Javascript

TS has many utility methods for arrays which are available via the prototype of Arrays. There are multiple which can achieve this goal but the two most convenient for this purpose are:

  1. Array.indexOf() Takes any value as an argument and then returns the first index at which a given element can be found in the array, or -1 if it is not present.
  2. Array.includes() Takes any value as an argument and then determines whether an array includes a this value. The method returning true if the value is found, otherwise false.

Example:

const channelArray: string[] = ['one', 'two', 'three'];

console.log(channelArray.indexOf('three'));      // 2
console.log(channelArray.indexOf('three') > -1); // true
console.log(channelArray.indexOf('four') > -1);  // false
console.log(channelArray.includes('three'));     // true

Solution 7 - Javascript

You can use filter too

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

Solution 8 - Javascript

do like this:

departments: string[]=[];
if(this.departments.indexOf(this.departmentName.trim()) >-1 ){
            return;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncode1View Question on Stackoverflow
Solution 1 - JavascriptbaaoView Answer on Stackoverflow
Solution 2 - JavascriptNitzan TomerView Answer on Stackoverflow
Solution 3 - JavascriptalejokoView Answer on Stackoverflow
Solution 4 - JavascriptDavid DehghanView Answer on Stackoverflow
Solution 5 - JavascriptBasiView Answer on Stackoverflow
Solution 6 - JavascriptWillem van der VeenView Answer on Stackoverflow
Solution 7 - JavascriptR15View Answer on Stackoverflow
Solution 8 - JavascriptAbdus Salam AzadView Answer on Stackoverflow