Skip to content Skip to sidebar Skip to footer

How Do I Rewrite The Search Function By Adding A For Loop To Go Through Data.games And Also Add When I Search That It Says The Score?

$(document).ready(function(){ var textsearch = 'XYZ' $.ajax({ dataType: 'jsonp', //data in jsonp contentType: 'application/json; charset=utf-8',

Solution 1:

Here is the Updated Fiddle

Just search for the word and fetch scores if there is a match and display

for (var key in text)
    { 
        if(text[key].hasOwnProperty('htn') && text[key].hasOwnProperty('hts'))
        {
            if(text[key]['htn'].toLowerCase().indexOf(textsearch.toLowerCase()) != -1)
            str= text[key]['htn']+ " vs "+ text[key]['atn']+ " score : "+ text[key]['hts']+"-"+text[key]['ats']+"\n";

            if( text[key]['atn'].toLowerCase().indexOf(textsearch.toLowerCase())!=-1)
            str += text[key]['atn'] + " vs "+ text[key]['htn']+ " score : "+ text[key]['ats']+"-"+text[key]['hts'];
        }
     }
    alert(str);

Solution 2:

if data.games is an array you could do

data.games.map(function(e){return e.score}); 

and you will get an array of scores. If you want all of the scores added up you could do...

data.games
.map(function(e){return e.score})
.reduce(function(prev,current){return prev+current});

If you want the teams and the score

data.games.map(function(game){
    return{
        team1: game.team1Name,
        team2: game.team2Name,
        score: game.score
    }
});

That will give you an array of objects with the attributes of both team names and the score

Post a Comment for "How Do I Rewrite The Search Function By Adding A For Loop To Go Through Data.games And Also Add When I Search That It Says The Score?"