How to assign the array key a value, when the key name is itself a variable

Posted by Matrym on Stack Overflow See other posts from Stack Overflow or by Matrym
Published on 2010-05-04T03:21:01Z Indexed on 2010/05/04 3:28 UTC
Read the original article Hit count: 478

Filed under:
|
|
|
|

How do I identify an item in a hash array if the key of the array is only known within a variable? For example:

var key = "myKey";
var array = {myKey: 1, anotherKey: 2};
alert(array.key);

Also, how would I assign a value to that key, having identified it with the variable?

This is, of course, assuming that I must use the variable key to identify which item in the array to alert.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about array