How do I check whether an array contains a string in TypeScript?
@lebyy▲ 0 net votes
Problem: 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'? Fix (from the top answer): 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…