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

后端 未结 8 1889
无人及你
无人及你 2020-12-12 11:42

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

var channelArray: Array = [\'one\', \'two\', \'three\'];

How ca

8条回答
  •  爱一瞬间的悲伤
    2020-12-12 12:01

    If your code is ES7 based:

    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.

提交回复
热议问题