我今天下午在做的練習上有些迷茫。 我有一些信息到一個JSON數組,像這樣:如何使用正則表達式搜索JSON
var json = [
{
"recipe": {
"URL": "www.google.com",
"Title": "theTitle",
"Time": "theTime",
"Ingredients": "... tomato potato orange ...",
"Image": "theImage"
}
},
{
"recipe": {
"URL": "www.google.com",
"Title": "theTitle",
"Time": "theTime",
"Ingredients": "... tomato orange potato and fish...",
"Image": "theImage"
}
},
{
"recipe": {
"URL": "www.google.com",
"Title": "theTitle",
"Time": "theTime",
"Ingredients": "... nothing ...",
"Image": "theImage"
}
},
{
"recipe": {
"URL": "www.google.com",
"Title": "theTitle",
"Time": "theTime",
"Ingredients": "... nothing ...",
"Image": "theImage"
}
}
]
而我試圖讓在JSON的位置,如果它與我的變量之一匹配。這裏是我想要做的一個例子:
var matches = ["tomato","potato"]
for (var i = 0; i < json.length;i++){
if (json[i].recipe.Ingredients == matches) {
alert("I got something :" + json[i])
}
else {
nothing}
}
所以,我試圖把它與一個正則表達式,但沒有奏效。 任何想法,我該怎麼做呢? 對不起,如果它可能看起來很愚蠢,我還是新來的編碼:D!
您可以使用[String.prototype.indexOf()]查看另一個子字符串是否存在(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf ) –
你有解決這個任務與'RegExp'?我認爲這不合適。 – Hitmands