Javascript Jquery逻辑按单词匹配数对选择选项进行排序
我想要类似于Mysql的数据排序,但是在jquery中。我有一个输入和一个选择标记,就像这样- 我希望选择选项根据输入文本的值进行过滤和排序 对输入文本应用过滤或逻辑,并保留至少包含一个单词的所有选项 排序/分组然后按匹配数对其余选项进行排序,使包含输入中所有单词的选项显示在顶部,然后是那些少一个单词的选项,依此类推。以下仅为说明,可能不准确- 我已经完成了过滤部分的逻辑。现在是排序部分,我不知道该怎么做 举个例子,假设输入文本发布了一个由5个单词组成的字符串。我不知道jQuery中是否有类似于Mysql的顺序的东西可以返回排序后的选项。所以,我的逻辑就像这个伪代码-Javascript Jquery逻辑按单词匹配数对选择选项进行排序,javascript,jquery,sorting,options,Javascript,Jquery,Sorting,Options,我想要类似于Mysql的数据排序,但是在jquery中。我有一个输入和一个选择标记,就像这样- 我希望选择选项根据输入文本的值进行过滤和排序 对输入文本应用过滤或逻辑,并保留至少包含一个单词的所有选项 排序/分组然后按匹配数对其余选项进行排序,使包含输入中所有单词的选项显示在顶部,然后是那些少一个单词的选项,依此类推。以下仅为说明,可能不准确- 我已经完成了过滤部分的逻辑。现在是排序部分,我不知道该怎么做 举个例子,假设输入文本发布了一个由5个单词组成的字符串。我不知道jQuery中是否有类似于
var numberOfWords;
sortedOptions = new Array();
for(cnt=numberOfWords; cnt>0; cnt --)
{
find options containing exactly those many words
append them to array sortedOptions
}
现在考虑NoWorkWord=5和CNT=3的情况。3个单词有很多可能的组合,我需要检查这些组合以准备3个单词匹配的选项。这很好,但是当字数增加时,代码的时间复杂度又如何呢?有更好的优化方法吗
请注意-此检查可能需要在用户键入每个键时进行,我不能如此频繁地访问后端数据库。我还没有找到任何用于相同目的的现成插件。请检查我之前关于这个的问题。如果你知道任何插件可以解决这个问题,请张贴在那里。但无论如何,我们都期待着解决这个问题谢谢类似这样的东西并不完全有效:
$(function(){
var $select = $('#mySelectBox'), nonMatches = [], $text = $('#myTextBox').keyup(function(){
// find all the words
var words = $text.val().split(' '), options = []
// nonMatches is an array of <option>s from the prev search that didn't match
// we put them back in the <select>
for (var i in nonMatches)
$select.append(nonMatches[i])
nonMatches = []
// and clear all the old labels like "1 word match"
$select.find('optgroup').each(function(){
var $this = $(this)
$this.replaceWith($this.html())
})
// if the textbox is blank, dont need to search
if (!words.length)
return
// loop thru each <option>
$select.find('option').each(function(){
var wordCount = 0, $this = $(this), html = ' ' + $this.html().toLowerCase() + ' '
// loop thru each word and check if the <select> contains that word
for (var i in words) {
if (html.indexOf(' ' + words[i].toLowerCase() + ' ') > -1)
wordCount++
}
// if this <option> doesn't have any of the words, save and remove it
if (wordCount == 0)
nonMatches.push($this.remove())
else {
// otherwise, save it to be sorted
if (!options[wordCount])
options[wordCount] = []
options[wordCount].push($this.remove())
}
})
// the options array holds all the <option>s that match; we need to sort it
keys = [], sortedOptions = []
for (var i in options) {
keys.push(i)
}
keys.sort()
keys.reverse()
for (var i in keys)
sortedOptions[keys[i]] = options[keys[i]]
for (var i in sortedOptions) {
// put the matches in the <select>
$select.append('<optgroup label="' + (i == words.length ? 'All' : i) + ' word match">')
for (var j in sortedOptions[i]) {
$select.append(sortedOptions[i][j])
}
$select.append('</optgroup')
}
})
})
您可以使用数组过滤器过滤出结果。这将为您提供一个子元素数组 您可以使用sort函数按长度排序
当然,亚伯拉罕谢谢你的尝试,你也可以告诉我你是如何思考的伪代码…好吧,你不需要检查每个单词的组合。就像循环通过每个,然后对于每个循环通过所有输入的单词,计算它包含多少,等等。你是正确的,循环通过选项而不是每个单词的组合会更快。需要检查你的代码,这是相当长的。。。你能给我写一个解释或者伪代码吗?虽然我做了一些修改,但这是可行的。稍后将共享代码。A+1适合你。每个人都投票给这家伙。。。真棒的努力。。。我会分析最好的答案…嗯,我想我需要检查一下。排序的事情。那么,看起来对每一组连续的数组键都会调用sort函数?在这种情况下,如果我用输入字检查这两个连续值的匹配数并返回基于该值的差值,那么最后我得到了一个排序数组?是吗?你可以把整个单词表排序。当您回显它时,您可以根据需要添加到opt组中
function isBigEnough(element, index, array) {
return (element >= 10);
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]
var numbers = ["aa", "b"];
numbers.sort(function(a, b) {
return a.length - b.length;
});