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:
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.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