Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript搜索字符串匹配,不显示重复项_Javascript_Xml_Match_Indexof - Fatal编程技术网

Javascript搜索字符串匹配,不显示重复项

Javascript搜索字符串匹配,不显示重复项,javascript,xml,match,indexof,Javascript,Xml,Match,Indexof,这是我的XML <document> <item> <my_summary>The cow jump over the moon</my_summary> </item> <item> <my_summary>Pepper piper keeper moon</my_summary> </item> <item>

这是我的XML

<document>
    <item>
    <my_summary>The cow jump over the moon</my_summary>
    </item>
    <item>
    <my_summary>Pepper piper keeper moon</my_summary>
    </item>
    <item>
    <my_summary>The swan flew jump the lake</my_summary>
    </item>
    <item>
    <my_summary>moon stars blue yellow</my_summary>
    </item>
    </document>
我的目标结果是删除重复项。。。在本例中,删除“奶牛跳过月亮”


如何通过“over”和“moon”进行搜索,如果有匹配项,则不显示重复项。

我会以不同的方式进行搜索。我将使用jquery获取我需要的元素,然后您可以根据需要输出它们。filter函数检查每个元素以匹配过滤词。这样,您就不会有重复项,因为它只对每个元素迭代一次(由于for循环,在内部可能会迭代更多)

var count_my_summary =xmlDoc.getElementsByTagName("item");
var keywords = "Over moon";
var filtered_array_exclude = keywords.match(/\w+/g).map(function(i) {return i.toLowerCase();}).filter(function(i) { return exclusionlist.indexOf(i) == -1; }) 
var filtered_array = new removeDuplicateElement(filtered_array_exclude);


for (xml_i=0;xml_i<count_my_summary.length;xml_i++){ 
     for (var i = 0; i < filtered_array.length; i++) {
        var xml_summary_lowercase = count_my_summary[xml_i].getElementsByTagName("my_summary")[0].childNodes[0].nodeValue.toLowerCase();
        var result = xml_summary_lowercase.indexOf(keyword,0);
        if (result > -1) {
        document.write(my_summary + "<br><br>");
        } else {
        document.write("");
        }
    }   
  }
}
The cow jump over the moon - for "over" match
The cow jump over the moon - for "moon" match
Pepper piper keeper moon - for "moon" match
moon stars blue yellow - for "moon" match
The cow jump over the moon - for "over " and "moon" match
Pepper piper keeper moon - for "moon" match
moon stars blue yellow - for "moon" match
var filterString = "moon over";
var filterArr = filterString.split(" ");

$(xmlDoc).find("my_summary").filter(function(){
for(var str in filterArr){
if ($(this).text().search(str) > -1)
return true;
}
return false;
});