私は今朝行ってきたエクササイズで少し失われています。 は、私はこのように、JSONの配列にいくつかの情報を持っている:Regexを使って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