how to call something specific from a rest api

I'm trying to call a specific stat from a rest API.

    {"5908":[{"name":"Riven's Cutthroats","tier":"CHALLENGER","queue":"RANKED_SOLO_5x5","entries":[{"playerOrTeamId":"5908","playerOrTeamName":"Dyrus","division":"I","leaguePoints":727,"wins":373,"losses":313,"isHotStreak":false,"isVeteran":true,"isFreshBlood":false,"isInactive":false}]},{"name":"Lee Sin's Soldiers","tier":"PLATINUM","queue":"RANKED_TEAM_5x5","entries":[{"playerOrTeamId":"TEAM-46c2da55-7d0d-11e5-91f5-c81f66ddabda","playerOrTeamName":"RetiredCrabbyPpl","division":"III","leaguePoints":0,"wins":4,"losses":2,"isHotStreak":false,"isVeteran":false,"isFreshBlood":false,"isInactive":false}]}]}

I want to get the "tier": value (Challenger in this case), but only if the "queue": type is "RANKED_SOLO_5X5".

function getStuff() {
    var SUMMONER_ID = "";
    SUMMONER_ID = $('#theKey').val();

    if(SUMMONER_ID !== 0) {

    $.ajax({
        url: 'https://na.api.pvp.net/api/lol/na/v2.5/league/by-summoner/' + SUMMONER_ID + '/entry?api_key={KEY}' ,
        type: 'GET',
        dataType: 'json',
        async: false,
        data: {

        },
        success: function (json) {
          var user = SUMMONER_ID;

          var summonerRank = json[user].tier;

          document.getElementById("sRank").innerHTML = summonerRank;

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert("error getting Summoner rank!");
        }
    });
  } else{}
}

right now i have this code but the only thing I get back is undefined. (The key I use has been hidden).

Answers


The value at json[user] is an array. What your code is currently doing is trying to access a property named tier of the array object instead of one of the objects in the array.

You need to loop through the array and then test each object for your value

for(var i=0; i<json[user].length; i++){
   if(json[user][i].queue == 'RANKED_SOLO_5X5'){
      console.log(json[user][i].tier);
   }
}

Need Your Help

Google Cloud Datastore with and w/o Protocol Buffers

google-app-engine protocol-buffers google-cloud-datastore

While searching for information about the Google Cloud Datastore, I stumbled upon two different official documentation branches that seem to have the same content in terms of concepts, but provide ...

Ruby on Rails Group by columns and sum

ruby-on-rails ruby activerecord

I've got the following Ruby code in my Rails application: