after progging php since 3 years, im hanging at javascript. Is it possible to get value of an assoziative array with a variable? Example:
var a = new Array();
a["ANDI"] = "USER";
var test = "ANDI";
alert(a[test]);
Any suggestions, how I could workaround that? Maybe with objects?
Thx for help!
Arrays are indexed by numbers. Objects have properties that can be accessed by name.
var myContainer = {
'User': 'Andy'
};
var key = 'User';
myContainer[key]; // Returns 'Andy'.
Yup. That should alert USER.
But JavaScript has objects. If you want to do that, you'd probably want..
var a = {
"ANDI": "USER"
};
For more details of JavaScript's object notations, check out JSON.org.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With