2013-04-10 14:58:50 -07:00
|
|
|
var assoc = {};
|
2015-11-18 06:14:39 +00:00
|
|
|
|
2013-04-10 14:58:50 -07:00
|
|
|
assoc['foo'] = 'bar';
|
|
|
|
|
assoc['another-key'] = 3;
|
|
|
|
|
|
2015-11-18 06:14:39 +00:00
|
|
|
// dot notation can be used if the property name is a valid identifier
|
|
|
|
|
assoc.thirdKey = 'we can also do this!';
|
|
|
|
|
assoc[2] = "the index here is the string '2'";
|
|
|
|
|
|
|
|
|
|
//using JavaScript's object literal notation
|
|
|
|
|
var assoc = {
|
|
|
|
|
foo: 'bar',
|
|
|
|
|
'another-key': 3 //the key can either be enclosed by quotes or not
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//iterating keys
|
2013-04-10 14:58:50 -07:00
|
|
|
for (var key in assoc) {
|
2015-11-18 06:14:39 +00:00
|
|
|
// hasOwnProperty() method ensures the property isn't inherited
|
2013-04-10 14:58:50 -07:00
|
|
|
if (assoc.hasOwnProperty(key)) {
|
|
|
|
|
alert('key:"' + key + '", value:"' + assoc[key] + '"');
|
|
|
|
|
}
|
|
|
|
|
}
|