javascript - Access random object on parsing JSON via combined variable -
this question has answer here:
i'm having simple json file looks like:
{ "seasons" : { "s_1" : { "episodes" : 7 }, "s_2" : { "episodes" : 21 }, "s_3" : { "episodes" : 22 }, "s_4" : { "episodes" : 30 }, "s_5" : { "episodes" : 18 }, "s_6" : { "episodes" : 12 } } }
and want randomly select s_ x
value seasons when parsing file:
var random = math.floor(math.random() * 6) + 1; $.getjson('js/data.json', function(data) { console.log(data.seasons.s_+random); });
this not work. how correct way? thanks
does work?
console.log(data.seasons["s_"+random]);
using brackets becomes data.seasons["s_1"]
Comments
Post a Comment