Reputation: 23
I am stuck with getting my head wrapped around some advanced search feature I need to implement. I have a search form with movie titles, actor, director, country and genre and I need to be able to search for say title and actor, director, country and genre and so on.
My problem is: How do I go about implementing this, if I don't know beforehand which data the user is searching for?
I have movie objects of this form in an array:
{
"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 andre vinteren 1943, da fiskeb\u00e5ten deres angripes av en tysk patruljeb\u00e5t. Som eneste overlevende lykkes det Baalsrud \u00e5 unnslippe og en farefull reise mot n\u00f8ytrale Sverige begynner. Etter flere uker i sn\u00f8massene i de steile norske fjellene blir han sn\u00f8blind og m\u00e5 til slutt skj\u00e6re av seg sine frostskadde t\u00e6r for \u00e5 overleve. \n\nDen mest fremtredende norske filmregiss\u00f8r etter 2. verdenskrig, Arne Skouen, har med denne filmen laget et mesterverk, som ble nominert til Oscar for beste utenlandske film i 1957, og siden k\u00e5ret til Norges beste film gjennom tidene. Det vidunderlige norske landskapet danner bakgrunn for den sanne historien om Jan Baalsruds utrolige flukt fra den tyske h\u00e6r. Jack Fjeldstad (Gull Og Gr\u00f8nne Skoger) spiller Baalsrud med imponerende overbevisning, og ble i 1985 sl\u00e5tt til ridder av St. Olavordenen for sitt fremragende arbeid i norsk filmkunst. ",
"dir": "Arne Skouen",
"etitle": "Nine Lives",
"country": "NOR",
"imdb_id": "",
"folk": "Jack Fjeldstad, Henny Moan, Alf Malland, Joachim Holst-Jensen, Lydia Op\u00f8ien, Edvard Drabl\u00f8s"
}
And the query params are of this form
{
film_title:"King Kong",
director:"",
genre:"action",
country: "",
actor: "Jack Black"
}
Do I really need to write 25 different if-else-blocks which will check for each possible case of search? That seems tedious and error prone.
Any tips or ideas that can push me in the right direction is very much appreciated!
Upvotes: 0
Views: 1070
Reputation: 386868
You could use the same keys for the query as in the data object and filter the result by iterating the keys of query and check if an element contains the wanted text.
function getItems(array, search) {
return array.filter(function (a) {
return Object.keys(search).every(function (k) {
return !search[k] || a[k].toLowerCase().indexOf(search[k].toLowerCase()) !== -1;
});
});
}
var data = [{ year: 1957, film_title: "King Kong", director: "", genre: "action", country: "", actor: "Jack Black" }, { year: 2000, film_title: "Foo", director: "", genre: "action", country: "", actor: "" }],
query = { film_title: "King Kong", director: "", genre: "action", country: "", actor: "Jack Black" },
result = getItems(data, query);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1
Reputation: 6112
Not completely sure what you want, but if you want to keep databases out of the picture:
function multiFilterSearch(arrayOfMovieObjs, queryParamObj){
var filteredSearch = arrayOfMovieObjs;
for(var key in queryParamObj){
filteredSearch = filteredSearch.filter(function(movieObj) {
// Throw away movieObj that lacks the search key altogether
if(typeof movieObj[key] === undefined) return false;
// For movieObjs with the search key, keep only those where the search value matches the query.
else if(movieObj[key] === queryParamObj[key]) return true;
else return false;
});
}
return filteredSearch;
}
This has poor efficiency (ie. it runs the filtering process as often as there are search keys, rather than concatenating the conditions for the filter()
), which could be improved upon. However, my lunch break is over so I can't dwell on it!
Upvotes: 0