2015-11-05 194 views
-3

我有一個由5個字符串組成的數組。我的Javascript文件被鏈接到一個HTML文件,以便從用戶檢索一個字符串。如果用戶輸入的字符串與我的數組中的5個項目中的1相匹配,那麼它將返回「做得好」或某種類型的東西。如何在Javascript中搜索用戶輸入的字符串

我需要知道我如何搜索數組中的字符串,我從用戶那裏接收了字符串。

任何幫助,非常感謝。

感謝

回答

4

你可以使用.indexOf

var strings = ["hello", "world", "foo", "bar"]; 
function existsInStrings(str) { 
    return strings.indexOf(str) !== -1; 
} 
console.log(existsInStrings("hello")); //true 
console.log(existsInStrings("there")); //false 
相關問題