所以我有一个随机的javascript数组...
[@ larry,@ nicholas,@ notch]等
它们都以@符号开头.我想用Levenshtein距离对它们进行排序,以便列表顶部的那些最接近搜索项.目前,我有一些使用jQuery的javascript,.grep()使用javascript .match()方法围绕按键输入的搜索词:
(自首次发布以来编辑的代码)
limitArr = $.grep(imTheCallback, function(n){
return n.match(searchy.toLowerCase())
});
modArr = limitArr.sort(levenshtein(searchy.toLowerCase(), 50))
if (modArr[0].substr(0, 1) == '@') {
if (atRes.childred('div').length < 6) {
modArr.forEach(function(i){
atRes.append('<div class="oneResult">' + i + '</div>');
});
}
} else if (modArr[0].substr(0, 1) == '#') {
if (tagRes.children('div').length < 6) {
modArr.forEach(function(i){
tagRes.append('<div class="oneResult">' + i + '</div>');
});
}
}
$('.oneResult:first-child').addClass('active');
$('.oneResult').click(function(){
window.location.href = 'http://hashtag.ly/' + $(this).html();
});
Run Code Online (Sandbox Code Playgroud)
它还有一些if语句,用于检测数组是否包含主题标签(#)或提及(@).忽略这一点.这imTheCallback是名称数组,无论是主题标签还是提及,然后modArr是排序的数组.然后.atResults, …