2017-03-14 18 views
51

Obecnie używam kątowego 2.0. Mam tablicę w następujący sposób:Jak sprawdzić, czy tablica zawiera ciąg znaków w maszynopisie?

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

Jak mogę sprawdzić w maszynopisie czy channelArray zawiera ciąg "trzy"?

+6

powinno być 'channelArray: string []' –

+1

Możliwy duplikat [W jaki sposób mogę sprawdzić, czy tablica zawiera obiekt w JavaScript? ] (http://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) –

Odpowiedz

111

takie same jak w js, przy użyciu indexOf()

console.log(channelArray.indexOf("three") > -1); 

lub korzystając ES6 Array.prototype.includes()

console.log(channelArray.includes("three")); 
49

Można użyć some method:

console.log(channelArray.some(x => x === "three")); // true 

Można użyć find method:

console.log(channelArray.find(x => x === "three")); // three 

Albo można użyć indexOf method:

console.log(channelArray.indexOf("three")); // 2 
Powiązane problemy