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

Content deleted Content added
Reverted 1 edit by 184.101.99.76 (talk) to last revision by Jerryobject
Tags: Reverted section blanking
Line 590:
====Map and WeakMap====
Modern JavaScript handles associative arrays, using the <code>Map</code> and <code>WeakMap</code> classes. A map does not contain any keys by default; it only contains what is explicitly put into it. The keys and values can be any type (including functions, objects, or any primitive).
 
=====Creation=====
A map can be initialized with all items during construction:
 
<syntaxhighlight lang="javascript">
const phoneBook = new Map([
["Sally Smart", "555-9999"],
["John Doe", "555-1212"],
["J. Random Hacker", "553-1337"],
]);
</syntaxhighlight>
 
Alternatively, you can initialize an empty map and then add items:
 
<syntaxhighlight lang="javascript">
const phoneBook = new Map();
phoneBook.set("Sally Smart", "555-9999");
phoneBook.set("John Doe", "555-1212");
phoneBook.set("J. Random Hacker", "553-1337");
</syntaxhighlight>
 
=====Access by key=====