简体   繁体   English

JavaScript自定义搜索功能无效

[英]JavaScript custom search function not working in IE

Hi i am trying to create a custom search for a json array 嗨,我正在尝试为json数组创建自定义搜索

My code works fine in chrome and firfox but not in ie. 我的代码在chrome和firfox中运行良好,但在ie中没有。

the code works as follows. 代码的工作原理如下。 It loops trough each search term 'needle' and if it finds the word in the question it adds it to the array. 它遍历每个搜索词“ needle”,如果在问题中找到该词,则会将其添加到数组中。 if it finds a different search term in the same question is the hit counter gets incremented. 如果它在同一问题中找到其他搜索词,则命中计数器将递增。

All the code is here and can be run. 所有代码都在这里,可以运行。

The error i get is: 我得到的错误是:

Line:24
Char:3
Error:qna_questions[..].q is null or not an object

qna_questions = [
{ 'q':'this is a very good question','id':'1'},
{ 'q':'i like pllo in the summer','id':'2'},
{ 'q':'it rains allot in the summer mushroom','id':'3'},
{ 'q':'i love people and straberry cake','id':'4'},
{ 'q':'i love people and berry rain','id':'5'},
{ 'q':'dsff sd fsd rains sdfsd fsd ','id':'6'},
{ 'q':'the dog is fat','id':'7'},
];

var search_result = new Array();
var needle = "dog fat summer allot mushroom";
var needle = needle.split(' ');
for(j = 0; j < qna_questions.length; j++) {

    for(i =0; i < needle.length; i++) {

        if(qna_questions[j].q.search(needle[i].toLowerCase()) != -1 ) {

            if( search_result[qna_questions[j].id] === undefined ) {
                search_result[qna_questions[j].id] = { 
                        'q':qna_questions[j].q,
                        'id':qna_questions[j].id,
                        'hits':0
                        };
            } else {
                search_result[qna_questions[j].id].hits++;


            }

        }
    }
}
search_result.sort(function(a,b) { return parseInt(b.hits) - parseInt(a.hits) } );
for (x in search_result)
  {
  alert(search_result[x].q + ": "+search_result[x].hits +"<br />");
  }
qna_questions = [ 
{ 'q':'this is a very good question','id':'1'}, 
{ 'q':'i like pllo in the summer','id':'2'}, 
{ 'q':'it rains allot in the summer mushroom','id':'3'}, 
{ 'q':'i love people and straberry cake','id':'4'}, 
{ 'q':'i love people and berry rain','id':'5'}, 
{ 'q':'dsff sd fsd rains sdfsd fsd ','id':'6'},
{ 'q':'the dog is fat','id':'7'}, 
]; 

Note the extra comma at the end of qna_questions ... in IE that will be one more null entry at the end of the array.... remove the comma and you should be good. 注意qna_questions结尾处的额外逗号...在IE中将在数组末尾再一个空条目....删除逗号,你应该是好的。

qna_questions = [ 
{ 'q':'this is a very good question','id':'1'}, 
{ 'q':'i like pllo in the summer','id':'2'}, 
{ 'q':'it rains allot in the summer mushroom','id':'3'}, 
{ 'q':'i love people and straberry cake','id':'4'}, 
{ 'q':'i love people and berry rain','id':'5'}, 
{ 'q':'dsff sd fsd rains sdfsd fsd ','id':'6'},
{ 'q':'the dog is fat','id':'7'}
]; 

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

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