how to get the value 'dynamically' from an associative array in javascript? -
i trying value key stored on string variable proynombre, whenever call via common method "myassociativearray.mykey", gets variable 'proynombre' key, instead of getting value , passing key.
proyectos.each(function(index){ var proynombre = this.value; if(!(proynombre in myarray)){ // whenever variable undefined, define myarray[proynombre] = horas[index].value-0 + minutos[index].value/60; } else{ console.log(myarray.proynombre); //this doesnt work, tries give me value key 'proynombre' instead of looking proynombre variable console.log(myarray.this.value); //doesnt work either } });
try:
console.log(myarray[proynombre]); myarray object in javascript. can access object properties object.propertyname or, object['propertyname']. if variable proynombre contained name of property (which does) can use second form, did above. object.proynombre invalid - proynombre variable. can't example:
var myobject = {}; myobject.test = 'test string'; var s = 'test'; console.log(myobject.s); // wrong!! but do:
console.log(myobject.test); console.log(myobject['test']); console.log(myobject[s]);
Comments
Post a Comment