i am trying to get a value from a key stored on a string variable proyNombre, but whenever i call it via the common method “myAssociativeArray.MyKey”, it gets the variable ‘proyNombre’ as the key, instead of getting its value and passing it as a key.
proyectos.each(function(index){
var proyNombre = this.value;
if(!(proyNombre in myArray)){ // whenever the variable is undefined, define it
myArray[proyNombre] = horas[index].value-0 + minutos[index].value/60;
}
else{
console.log(myArray.proyNombre); //This doesnt work, it tries to give me the value for the key 'proyNombre' instead of looking for the proyNombre variable
console.log(myArray.this.value); //doesnt work either
}
});
Try:
myArray is actually an object in javascript. You can access object properties with
object.propertyNameor,object['propertyName']. If your variableproyNombrecontained the name of a property (which it does) you can use the second form, like I did above.object.proyNombreis invalid – proyNombre is a variable. You can’t do for example:but you could then do: