Currently I am using Angular 2.0. I have an array as follows:
目前我使用的是角2。0。我有如下数组:
var channelArray: Array<string> = ['one', 'two', 'three'];
How can I check in TypeScript whether the channelArray contains a string 'three'?
我如何检查打字稿中channelArray是否包含一个字符串“three”?
3 个解决方案
#1
158
The same as in JavaScript, using Array.prototype.indexOf():
与JavaScript相同,使用Array.prototype.indexOf():
console.log(channelArray.indexOf('three') > -1);
Or using ECMAScript 6 Array.prototype.includes():
或使用ECMAScript 6 Array.prototype.includes():
console.log(channelArray.includes('three'));
#2
69
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:
或者你可以使用indexOf方法:
console.log(channelArray.indexOf("three")); // 2
#3
0
console.log("three" in channelArray) // this will return a boolean value.
控制台。log(“3”在channelArray中)//这将返回一个布尔值。
#1
158
The same as in JavaScript, using Array.prototype.indexOf():
与JavaScript相同,使用Array.prototype.indexOf():
console.log(channelArray.indexOf('three') > -1);
Or using ECMAScript 6 Array.prototype.includes():
或使用ECMAScript 6 Array.prototype.includes():
console.log(channelArray.includes('three'));
#2
69
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:
或者你可以使用indexOf方法:
console.log(channelArray.indexOf("three")); // 2
#3
0
console.log("three" in channelArray) // this will return a boolean value.
控制台。log(“3”在channelArray中)//这将返回一个布尔值。