Associative array/Creation: Difference between revisions

Line 1,107:
 
=={{header|JavaScript}}==
JavascriptECMAScript5.1 does not have associative arrays, however Objects (which are just an unordered bundle of name/value pairs) can be used like associative arrays. JavascriptJavaScript Arrays, whichmay are just objects with a special length property and some handy inherited methods (such as pop, shift, slice, etc.), canalso be used as associative arrays too. However, by convention,but Objects are most often used. Arrays are only used where access by numeric index is needed (such as when accessing values in numeric order) and use of the special length property is helpful (such as iterating from zero to array.length)convention.
 
Javascript object property names (keys) are strings. Other types and expressions can be used with square bracket notation, they are evaluated and converted to strings and the result used as the property name:. Using quotes on property names avoids potential collisions with reserved JavaScript key words.
<lang javascript>var assoc = {};
 
assoc['foo'] = 'bar';
assoc['another-key'] = 3;
assoc.thirdKey = 'we can also do this!'; // dot notation can be used if the property name
// is a valid identifier
assoc[2] = 'the index here is the string "2"';
assoc[null] = 'this also works';
assoc[(function(){return 'expr';})()] = 'Can use expressions too';
 
// dot notation can be used if the property name is a valid identifier
assoc.thirdKey = 'we can also do this!'; // dot notation can be used if the property name
assoc[2] = '"the index here is the string "'2"'";
 
//using JavaScript's object literal notation
<lang javascript>var assoc = {
foo: 'bar',
'another-key': 3 //the key can either be enclosed by quotes or not
};
 
//iterating keys
for (var key in assoc) {
The // hasOwnProperty() method can be used to ensure thatensures the property is on the object and notisn't inherited.
if (assoc.hasOwnProperty(key)) {
alert('key:"' + key + '", value:"' + assoc[key] + '"');
Line 1,125 ⟶ 1,133:
}</lang>
 
ECMAScript 6 (ES6) offers both a map and a weak map implementation. While Objects must use strings, Maps may use objects, functions, and numbers as keys in addition to strings.
The hasOwnProperty method can be used to ensure that the property is on the object and not inherited.
<lang javascript>'foo'var inmap assoc= //new true</lang>Map(),
fn = function () {},
obj = {};
 
map.set(fn, 123);
The above associative array can also be constructed using javascript's object literal notation
map.set(obj, 'abc');
<lang javascript>var assoc = {
map.set('key', 'val');
foo: 'bar',
map.set(3, x => x + x);
'another-key': 3 //the key can either be enclosed by quotes or not
};</lang>
 
map.get(fn); //=> 123
Using quotes on property names avoids potential collisions with reserved JavaScript key words.
map.get(function () {}); //=> undefined because not the same function
http://www.quackit.com/javascript/javascript_reserved_words.cfm
map.get(obj); //=> 'abc'
map.get({}); //=> undefined because not the same object
map.get('key'); //=> 'val'
map.get(3); //=> (x => x + x)
 
map.size; //=> 4
The in operator can be used to check whether an object has a property with a particular name:
 
<lang javascript>'foo' in assoc // true</lang>
//iterating using ES6 for..of syntax
for (var key of map.keys()) {
console.log(key + ' => ' + map.get(key));
};</lang>
 
=={{header|jq}}==
Anonymous user