最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to get the value 'dynamically' from an associative array in javascript? - Stack Overflow

programmeradmin0浏览0评论

i am trying to get a value from a key stored on a string variable proyNombre, but whenever i call it via the mon 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
    }

});

i am trying to get a value from a key stored on a string variable proyNombre, but whenever i call it via the mon 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
    }

});
Share Improve this question edited Apr 26, 2012 at 11:42 casperOne 74.6k19 gold badges189 silver badges260 bronze badges asked Dec 3, 2010 at 1:54 CharChar 9552 gold badges10 silver badges26 bronze badges 1
  • Note that JavaScript doesn't have associative arrays - just objects. – alex Commented Dec 3, 2010 at 1:58
Add a ment  | 

4 Answers 4

Reset to default 2

Try:

console.log(myArray[proyNombre]);

myArray is actually an object in javascript. You can access object properties with object.propertyName or, object['propertyName']. If your variable proyNombre contained the name of a property (which it does) you can use the second form, like I did above. object.proyNombre is invalid - proyNombre is a variable. You can't do for example:

var myObject = {};
myObject.test = 'test string';

var s = 'test';
console.log(myObject.s); // wrong!!

but you could then do:

console.log(myObject.test);
console.log(myObject['test']);
console.log(myObject[s]);

You need to use the same syntax you used to set the value:

console.log(myArray[proyNombre]);

Simply access the value with myArray[proyNombre].

You're doing it right in the assignment: myArray[proyNombre]. You can use the same method to retrieve the variable.

If you change:

console.log(myArray.proyNombre);
console.log(myArray.this.value);

to

console.log(myArray[proyNombre]);
console.log(myArray[this.value]);

You should get the same value (the value for the key represented by the variable proyNombre) logged twice.

It's true that Javascript doesn't have associative arrays but objects in Javascript can be treated like associative arrays when accessing their members.

发布评论

评论列表(0)

  1. 暂无评论