Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
Tags: Reverted section blanking
Tag: Reverted
Line 616:
</syntaxhighlight>
 
same
====Object====
An object is similar to a map—both let you set keys to values, retrieve those values, delete keys, and detect whether a value is stored at a key. For this reason (and because there were no built-in alternatives), objects historically have been used as maps.
 
However, there are important differences that make a map preferable in certain cases. In JavaScript an object is a mapping from property names to values—that is, an associative array with one caveat: the keys of an object must be either a string or a symbol (native objects and primitives implicitly converted to a string keys are allowed). Objects also include one feature unrelated to associative arrays: an object has a prototype, so it contains default keys that could conflict with user-defined keys. So, doing a lookup for a property will point the lookup to the prototype's definition if the object does not define the property.
 
An object literal is written as <code>{ property1: value1, property2: value2, ... }</code>. For example:
 
<syntaxhighlight lang="javascript">
const myObject = {
"Sally Smart": "555-9999",
"John Doe": "555-1212",
"J. Random Hacker": "553-1337",
};
</syntaxhighlight>
 
To prevent the lookup from using the prototype's properties, you can use the <code>Object.setPrototypeOf</code> function:
 
<syntaxhighlight lang=JavaScript>
Object.setPrototypeOf(myObject, null);
</syntaxhighlight>
 
As of ECMAScript 5 (ES5), the prototype can also be bypassed by using <code>Object.create(null)</code>:
 
<syntaxhighlight lang=JavaScript>
const myObject = Object.create(null);
 
Object.assign(myObject, {
"Sally Smart": "555-9999",
"John Doe": "555-1212",
"J. Random Hacker": "553-1337",
});
</syntaxhighlight>
 
If the property name is a valid identifier, the quotes can be omitted, e.g.:
 
<syntaxhighlight lang=JavaScript>
const myOtherObject = { foo: 42, bar: false };
</syntaxhighlight>
 
Lookup is written using property-access notation, either square brackets, which always work, or dot notation, which only works for identifier keys:
 
<syntaxhighlight lang=JavaScript>
myObject["John Doe"]
myOtherObject.foo
</syntaxhighlight>
 
You can also loop through all enumerable properties and associated values as follows (a for-in loop):
 
<syntaxhighlight lang=JavaScript>
for (const property in myObject) {
const value = myObject[property];
console.log(`myObject[${property}] = ${value}`);
}
</syntaxhighlight>
 
Or (a for-of loop):
 
<syntaxhighlight lang=JavaScript>
for (const [property, value] of Object.entries(myObject)) {
console.log(`${property} = ${value}`);
}
</syntaxhighlight>
 
A property can be removed as follows:
 
<syntaxhighlight lang=JavaScript>
delete myObject["Sally Smart"];
</syntaxhighlight>
 
As mentioned before, properties are strings and symbols. Since every native object and primitive can be implicitly converted to a string, you can do:
 
<syntaxhighlight lang=JavaScript>
myObject[1] // key is "1"; note that myObject[1] == myObject["1"]
myObject[["a", "b"]] // key is "a,b"
myObject[{ toString() { return "hello world"; } }] // key is "hello world"
</syntaxhighlight>
 
In modern JavaScript it's considered bad form to use the Array type as an associative array. Consensus is that the Object type and <code>Map</code>/<code>WeakMap</code> classes are best for this purpose. The reasoning behind this is that if Array is extended via prototype and Object is kept pristine, for and for-in loops will work as expected on associative 'arrays'. This issue has been brought to the fore by the popularity of JavaScript frameworks that make heavy and sometimes indiscriminate use of prototypes to extend JavaScript's inbuilt types.
 
See [http://blog.metawrap.com/2006/05/30/june-6th-is-javascript-array-and-object-prototype-awareness-day/ JavaScript Array And Object Prototype Awareness Day] for more information on the issue.
 
===Julia===