Associative array/Creation: Difference between revisions

Javascript
(Maintenance is complete. Removed maintenance template.)
(Javascript)
Line 121:
for (Map.Entry<String, Integer> entry: map.entrySet())
System.out.println(entry.getKey() + " => " + entry.getValue());
 
==[[JavaScript]]==
[[Category:JavaScript]]
 
In Javascript we make an associative array from an empty object, otherwise if we make it from an array we'll get all the Array object's method and properties when we iterate over it
var assoc = {};
assoc['foo'] = 'bar';
assoc['another-key'] = 3;
assoc.thirdKey = 'we can also do this!';
for(key in assoc)
{
alert('key:"'+key+'", value:"'+assoc[key]+'"');
}
 
The above associative array can also be constructed using Javascript's object literal syntax
 
var assoc = {
foo:'bar',
'another-key':3, //the key can either be enclosed by quotes or not
thirdKey = 'we can also do this!'
};
 
==[[Lua]]==
Anonymous user