[英]JavaScript custom search function not working in IE
嗨,我正在尝试为json数组创建自定义搜索
我的代码在chrome和firfox中运行良好,但在ie中没有。
代码的工作原理如下。 它遍历每个搜索词“ needle”,如果在问题中找到该词,则会将其添加到数组中。 如果它在同一问题中找到其他搜索词,则命中计数器将递增。
所有代码都在这里,可以运行。
我得到的错误是:
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'},
];
注意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.