Currently, I am in the process of developing a search suggest feature that will provide the best match based on certain criteria. Below is the code snippet along with my explanatory comments.
/*
string = {"Canna Terra PLUS 50 Litres", "Canna Vega Tent", "Canna Bio Vega", "Super Canna 50 max" }
search = "Canna Vega" this can be dynamic ranging up to 4 words search term
The expected return array would be
{"Canna Vega Tent", "Canna Bio Vega" }
*/
function loadSuggest(string,search){
if( search.length < 3 ){
return; // suggest is loaded only if the search term is more than 3 letter
}
var terms = search.split(' '); // split the search term with spaces
var i;
for(i = 0; i < string.length; i++){
/*
how to dynamically check and return
the results containing more than one term match ?
I have tried indexOf() but that fails with dynamic number of words matching
*/
}
return resultArray;
}
Included within the comments of the code are my efforts towards achieving a return result that contains all the terms present in the search query for optimal matching.