javascript - Concatenating chosen random property to object to get the array? -


i have object few arrays in it. trying return 1 of arrays randomly. have function returns 1 of properties in object, not sure how use when concatenating object name. example:

object:

var myobj = {     itemone:['blue','red','green'],     itemtwo:['sky','grass','book'],     itemthree:['day','month','year'] }  

to array myobj.itemone return first array. want randomly return array. have function return itemone, itemtwo, or itemthree randomly, when concatenating function returns myobj. not work. can do. here function:

function pickrandomproperty(myobj) {     var result;     var count = 0;     (var prop in myobj){         if (math.random() < 1/++count){            result = prop;         }     }     return result; }  var getiteminobject = pickrandomproperty(myobj); 

using getiteminobject returns, try concatenate myobj return array. how that? here have tried:

var getiteminobject = pickrandomproperty(myobj); var randprop = myobj.getiteminobject; 

or even:

var randword = myobj + '.' + getiteminobject; 

which returns '[object object].itemtwo'

here fiddle: http://jsfiddle.net/xrk7b4zs/

thanks help

you use brackets notation:

var getiteminobject = pickrandomproperty(myobj); var randprop = myobj[getiteminobject]; //                  ^               ^ 

in javascript, can refer property using dot notation , property name literal (obj.foo), or brackets notation , property name string (obj["foo"]) (or symbol in es6+, that's not relevant here). in second case, string can result of expression, including variable reference.

live example:

var myobj = {      itemone:['blue','red','green'],      itemtwo:['sky','grass','book'],      itemthree:['day','month','year']  }     function pickrandomproperty(myobj) {      var result;      var count = 0;      (var prop in myobj){          if (math.random() < 1/++count){             result = prop;          }      }      return result;  }    var getiteminobject = pickrandomproperty(myobj);  var array = myobj[getiteminobject];  document.body.insertadjacenthtml(    "beforeend",    "<pre>" + json.stringify(array) + "</pre>"  );


side note: there's easier way pick random property, assuming care object's "own" properties (not ones prototype):

function pickrandomproperty(myobj) {     var keys = object.keys(myobj);     return keys[math.floor(math.random() * keys.length)]; } 

live example:

var myobj = {      itemone:['blue','red','green'],      itemtwo:['sky','grass','book'],      itemthree:['day','month','year']  }     function pickrandomproperty(myobj) {      var keys = object.keys(myobj);      return keys[math.floor(math.random() * keys.length)];  }    var getiteminobject = pickrandomproperty(myobj);  var array = myobj[getiteminobject];  document.body.insertadjacenthtml(    "beforeend",    "<pre>" + json.stringify(array) + "</pre>"  );


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -