我正在尝试构建一个搜索建议,返回最佳匹配项。以下是我的代码和注释:
/*
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;
}
我在代码注释中提到,我正在尝试找到一个包含搜索词中所有单词的字符串的最佳匹配。