javascript - Getting first object inside JSON -


i'll cut straight chase. i'm getting json object object inside of so:

function getname(summonername, region) {     lolapi.summoner.getbyname(summonername, region, function(err, summoner) {         if(!err) {             console.log(summoner);         }     }); } 

however, result of call (let's stay summonername "tetsii"):

{ tetsii:    { id: 51520537,     name: 'tetsii',     profileiconid: 23,     summonerlevel: 23,     revisiondate: 1408307600000   } } 

now, can access id's , stuff "console.log(summoner.tetsii.id)" example, because summonername (in case "tetsii") can anything, prefer not so. so, question is: how access first object inside json or there way? , no, can't array in case afaik.

i note i've tried "console.log(summoner.summonername.id)", doesn't yield results summonername string.

thanks everybody

edit: got answer. using summoner[summonername].id able grab id. answers!

-tetsii

by using object.keys. example, if know summoner have single top-level key, can with:

var summonername = object.keys(summoner)[0]; 

obligatory browser support notice: ie < 9 not support out of box, can use polyfill provided in mdn page compatibility shim.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -