2017-03-14 11 views
51

現在、私は角度2.0を使用しています。配列にtypescriptの文字列が含まれているかどうかを確認するには?

channelscrayに文字列 'three'が含まれているかどうかをチェックするにはどうすればよいですか?

+6

それは 'channelArrayする必要があります:文字列の[]' –

+1

可能な重複配列は、JavaScriptでのオブジェクトが含まれている場合は、[どのようにしてチェックしますか? ](http://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) –

答えて

111

jsのと同じで、 indexOf()を使用して

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

またはES6 Array.prototype.includesを使用して()

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

あなたはsome methodを使用することができます。

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

あなたはfind methodを使用することができます。

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

それともindexOf methodを使用することができます。

console.log(channelArray.indexOf("three")); // 2 
関連する問題