简体   繁体   English

JS-比较两个数组与indexOf

[英]JS - Comparing two arrays with indexOf

I'm trying to build a document search that accounts for fragments of words and different ordering of words - for instance, someone might be trying to find the section: 我正在尝试建立一个文档搜索,以说明单词的片段和单词的不同顺序-例如,某人可能正在尝试查找该部分:

Level 6 Pathology

which they might search for by inputting 他们可能会通过输入来搜索

Pathology Level 6
Level 6 Path
6 path

I also want to make sure that ALL the search terms are in the result so that 我还想确保结果中包含所有搜索词,以便

 level 6 path

doesn't open every single sub heading containing 'level' in the document. 不会在文档中打开包含“级别”的每个子标题。

To accomplish these requirements, I'm combining the search input strings "level" "6" "path" into every possible combination of space separated strings "level 6 path", "6 level path", "path 6 level" etc. 为了满足这些要求,我将搜索输入字符串“级别”“ 6”“路径”组合为空格分隔的字符串“级别6路径”,“ 6级路径”,“路径6级别”等的每种可能组合。

Code: 码:

function search(){
            event.preventDefault();
            var results = [];
            var searchinput = document.getElementById("searchbar").value.toUpperCase().split((" "), 6);                
            var word = searchinput.join(" ");
            if (searchinput.length > 1){
                var searchinputExpanded = recurCombinations(searchinput.length, searchinput);
                searchinputExpanded.unshift(word);
            } else {
                var searchinputExpanded = searchinput;
            }
            var arrayofdivs = document.querySelectorAll(".collapsible");                
            var j, k, m;

            //Find every combination of the search input so that both
            //Level 6 Path and Path Level 6 will find Level 6 Pathology
            function recurCombinations(len, input){
                var i = 0;
                var l = len - 1;

                if(len === 1){
                    return;
                } else {
                    for(i; i < l; i++){
                        recurCombinations(l, input);
                        l % 2 ? input.swap(i, l) : input.swap(0,l); //even-odd check 
                        results.push(input.join(" ").toString());
                    }
                    recurCombinations(l, input);
                }
                return results.sort();
            }            

            //close any currently expanded divs
            for(m = 0; m < arrayofdivs.length; m++){                    
                arrayofdivs[m].style.backgroundColor = "#eee";
                arrayofdivs[m].style.color = "#2CB2FF";
                arrayofdivs[m].nextElementSibling.style.display = "none";
            }

            //expand any applicable results
            for(j = 0; j < arrayofdivs.length; j++){
                for(k = 0; k < searchinputExpanded.length; k++){                      

                    if(arrayofdivs[j].innerHTML.toUpperCase().indexOf(searchinputExpanded[k]) !== -1){                                                 

                        arrayofdivs[j].style.backgroundColor = "#555";
                        arrayofdivs[j].style.color = "#fff";
                        arrayofdivs[j].nextElementSibling.style.display = "block";
                        arrayofdivs[j].parentNode.style.display = "block";
                        document.getElementById("clearbutton").style.display = "block";                
                    }
                }
            }
        }           

I've got this part working fine but some titles contain more words and the search doesn't account for this 我的这部分工作正常,但有些标题包含更多单词,但搜索并未说明这一点

Medical Imaging Level 5

Will be found by 将被发现

Level 5 Medical

but not 但不是

Level 5 Imaging

And in this instance, I think most people are going to search "Imaging level 5" instead of "Medical level 5" 在这种情况下,我认为大多数人会搜索“成像5级”而不是“医学5级”

I tried doing every combination of words for possible results - basically: 我尝试将所有单词组合在一起以获得可能的结果-基本上是:

 everyCombinationOfEveryPossibleResult.indexOf(everyCombinationOfSearchInput(path level 6) !== -1)

but the n! 但是之后! possibilities for each phrase when some possible result phrases are quite long meant it took too long (search input is capped at 6 strings for this reason) and would crash the browser. 当某些可能的结果短语很长时,每个短语的可能性就太长了(由于这个原因,搜索输入被限制为6个字符串),这会使浏览器崩溃。

Any help would be appreciated. 任何帮助,将不胜感激。

If I understand correctly, you want to only find strings that contain ALL of the words that are in the search term, but you don't want the order of the search terms to matter. 如果我理解正确,则只想查找包含搜索词中所有单词的字符串,但不希望搜索词的顺序很重要。 In that case, you can do something like this: 在这种情况下,您可以执行以下操作:

function searchMyArray(arrayToSearch, searchTermsArray){
    return arrayToSearch.filter(function(title){
        return !searchTermsArray.some(function(word){
            return title.indexOf(word) == -1;
        });
    });
}

In the example, arrayToSearch should be an array consisting of all the titles that you want to be searching through, and searchTermsArray should be an array of the search terms that you want to search for (if the search term comes in as a string, you can pretty easily divide it into an array with the String split method). 在该示例中,arrayToSearch应该是一个包含要搜索的所有标题的数组,searchTermsArray应该是要搜索的搜索项的数组(如果搜索项以字符串形式出现,则表示可以很容易地用String split方法将其分成一个数组)。 The function returns a list of titles that contains all of the search terms. 该函数返回包含所有搜索词的标题列表。

The function works by filtering out all of the titles where some word in the search term is not in the title. 该功能通过过滤掉搜索词中某些单词不在标题中的所有标题来起作用。

The function looks for the first word, if not there then continues into the next haystack option. 该函数查找第一个单词,如果没有,则继续到下一个干草堆选项。 If it finds the word, then it continues deeper, searching for the next word. 如果找到了该单词,则它将继续深入搜索下一个单词。

function searchForIndex(needleArr,haystackArr){
 var temp = needleArr;
 for(var i = 0; i < haystackArr.length;i++){
  if (haystackArr[i].indexOf(temp.shift())> -1){
   if (temp.length ==0) {
    return i;
   } else {
    return searchForIndex(temp,haystackArr);
   }
  }
 }
 return -1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM