使用它或类似的脚本,如何创建搜索脚本来搜索对象?

时间:2014-04-02 13:55:58

标签: javascript jquery ajax

$(document).ready(function(){
    $.ajax({
            dataType: 'jsonp', //data in jsonp
            contentType: "application/json; charset=utf-8",
            url: 'http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp',
            jsonpCallback: 'loadScoreboard', 
            success: function (data) {
               console.log(data);


        }
     });

--- 使用该基本代码,我现在如何为类似于此的搜索脚本编写脚本...

var text = ""; 

var times = 0; 

var textsearch = prompt("What would you like to search for?") 
for (var i = 0 ; i < text.length ; i++){ 
    if (text[i] == textsearch[0]){ 
        var okay = true;  
        for (var j = 1; j < textsearch.length; j++) { 
            if (textsearch[j] != text[i+j]) { 
                okay = false; 
                break; 
            }
        }
        if (okay == true){ 
            times += 1;    
        }
    }
}

console.log("Found " + textsearch + " " + times  + " times");

---- http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp搜索对象并搜索团队和分数?

1 个答案:

答案 0 :(得分:0)

以下是Working Fiddle

var textsearch = prompt("What would you like to search for?") 

if(textsearch)
{
    for (var key in text)
    { 
        if(text[key].hasOwnProperty('htn') && text[key].hasOwnProperty('atn'))
        {
            if((text[key]['htn'].toLowerCase()).indexOf(textsearch.toLowerCase()) != -1 || (text[key]['atn'].toLowerCase()).indexOf(textsearch.toLowerCase())!=-1)
             times ++;
        }
     }
}

alert("Found " + textsearch + " " + times  + " times");