gvdh
gvdh

Reputation: 75

How to search into a JSON using a Regex

I'm a bit lost on an exercice i've been doing this afternoon. I have some information into a JSON array, like this :

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

And I'm trying to get the position in the JSON if it matches with one of my variable. Here is an example of what i'm trying to do :

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

So I tried to it with a regex but it didn't work. Any idea of how am I supposed to do it ? Sorry if it might seems stupid, i'm still new to coding :D !

Upvotes: 1

Views: 56

Answers (3)

gvdh
gvdh

Reputation: 75

Thanks to both of you ! Last question, my JSON will get around 60K items. Do you think I can stay on those search methods or do I absolutely need to build an API ?

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22564

Here is my solution without the use of 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");
   }
  });
}

Upvotes: 0

gaetanoM
gaetanoM

Reputation: 42054

A solution can be:

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

where:

  1. matches.join('|') --> "tomato|potato"

  2. new RegExp(matches.join('|')) --> /tomato|potato/ (regex)

  3. json[i].recipe.Ingredients --> is the content of Ingredients

  4. test is the method executes a search for a match between a regular expression and a specified string. Returns true or 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');
    }
}

Upvotes: 2

Related Questions