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'?
arraysjavascripttypescript
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'?
Best Answer
The same as in JavaScript, using Array.prototype.indexOf():
Or using ECMAScript 2016 Array.prototype.includes():
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
Reference
Array.find()
Array.some()
Array.filter()