0

I'm making an advanced search function in my online video library. I get the information about the movies from the JSON-file object movies_object, and searching for the director, name of movie, country and actors works well. My problem is when I'm searching for genres. Genres is stored in a seperate JSON-file object, genres_object.

My problem is that I can't figure out how to find the key name in genres_object! I have the values, but I cant figure out how to find the keys. I have to do this so that the right genre goes to the right movie.

Here is my javascript function for advanced search

function advanced_search(){
var genres = genres_object;
var searchParameters = get_query_string_parameters();

for(movies in movies_object){
var found1 = false;
var found2 = false;
var found3 = false;
var found4 = false;
var found5 = false;

var id = movies_object[movies].id;


if(movies_object[movies].otitle.toLowerCase() == 
searchParameters.film_title.toLowerCase() || 
isEmpty(searchParameters.film_title)) {
found1 = true;
}

if(movies_object[movies].folk) {
if(movies_object[movies].folk.toLowerCase().includes(searchParameters.actor.toLowerCase()) || isEmpty(searchParameters.actor)) {
found2 = true;
}
}

//This is the part I'm having trouble with
if(genres_objects[movies] == movies_object[movies].id){
  for(var i = 0; i < genres_object[movies].length; i++){
if((genres_object[movies[i]]).includes(searchParameters.genre.toLowerCase())){
found5 = true;
}
}
} 


if(found1 && found2 && && found5) {
console.log(movies_object[movies]); 
results.push(movies_object[movies]);

}
display_X(results);
}
}

And here is the format for the JSON movie objects:

movies_object={"1080": {"year": 1957, "otitle": "Ni liv", "youtube trailer id": "", "ntitle": "Ni liv", "mod_date": null, "length": 91, "id": 1080, "keywords": null, "reg_date": null, "description": "Jan Baalsrud er p\u00e5 et sabotasjeoppdrag fra England til Norge med 11 a", "dir": "Arne Skouen", "etitle": "Nine Lives", "country": "NOR", "imdb_id": "", "folk": "Jack Fjeldstad, Henny Moan, Lydia Op\u00f8ien, Edvard Drabl\u00f8s"},

Lastly, the format of the JSON genres: genres_object={"3734": ["komedie"], "3724": ["sci-fi"],

Thanks in advance!

1 Answers1

0

You can easily iterate the keys in JSON object using

for (var key in genres_objects) {

    console.log(key);

}

or

// incase genres_objects[movies] returns JSON object

for (var key in genres_objects[movies]) {

    console.log(key);

}

Hope this helps :)

Purple Hexagon
  • 3,538
  • 2
  • 24
  • 45
rahulb
  • 63
  • 1
  • 6