2017-06-29 13 views
1

私は今朝行ってきたエクササイズで少し失われています。 は、私はこのように、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!

+0

[String.prototype.indexOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf)でサブストリングが他のサブストリングに存在するかどうかを確認できます。 ) –

+0

このタスクを 'RegExp'で解決してもらえますか?私はこれが適切ではないと思う。 – Hitmands

答えて

2

ソリューションをすることができます:

new RegExp(matches.join('|')).test(json[i].recipe.Ingredients) 

  1. matches.join( '|') - > "トマト|ポテト"

  2. 新しい正規表現(マッチ.join( '|')) - >/tomato |ジャガイモ/(正規表現)

  3. json [i] .recipe.Ingredients - >は内容です成分の数

  4. testこのメソッドは、正規表現と指定された文字列の一致を検索します。 trueまたはfalseを返します。ここで

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" 
 
     } 
 
    } 
 
]; 
 
var matches = ["tomato", "potato"] 
 

 
for (var i = 0; i < json.length; i++) { 
 
    if (new RegExp(matches.join('|')).test(json[i].recipe.Ingredients)) { 
 
     console.log("I got something :" + JSON.stringify(json[i])) 
 
    } 
 
    else { 
 
     console.log('nothing'); 
 
    } 
 
}

0

Regexを使用せずに私のソリューションです。あなたの両方に

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" 
 
    } 
 
    } 
 
] 
 

 

 
var matches = ["tomato","potato"] 
 

 
for (var i = 0; i < json.length;i++){ 
 
    matches.forEach(function(word){ 
 
    if(json[i].recipe.Ingredients.indexOf(word) > -1){ 
 
    console.log(json[i]); 
 
    } else { 
 
    console.log("doesn't exist"); 
 
    } 
 
    }); 
 
}

0

ありがとう! 最後の質問では、JSONは約60,000アイテムを取得します。私はそれらの検索方法にとどまることができると思いますか、あるいは私は絶対にAPIを構築する必要がありますか?

関連する問題