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

后端 未结 8 1884
无人及你
无人及你 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 11:59

    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
    

提交回复
热议问题