Revision 19284 of Map

  • Revision slug: JavaScript/Reference/Global_Objects/Map
  • Revision title: Map
  • Revision id: 19284
  • Created:
  • Creator: Marcoos
  • Is current revision? No
  • Comment 168 words added, 1 words removed

Revision Content

{{ fx_minversion_header("12") }}

{{ Non-standard_header() }}

Introduction

Map objects are simple key/value maps. Keys can be primitive values or object references.

Keys are considered equal if their value is indistinguishable.

API

Method Description
myMap.get(key) Returns the value associated to the key, or undefined if there is none.
myMap.set(key, value) Sets the value for the key  in myMap. Returns undefined.
myMap.has(key) Returns a boolean asserting whether a value has been associated to the key in myMap or not
myMap.delete(key) Removes any value associated to the key. After such a call, myMap.has(key) will return false.

Examples

var myMap = new Map();

var keyObj = {},
    keyFunc = function () {},
    keyString = "a string";

// setting the values
myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, "value associated with keyObj");
myMap.set(keyFunc, "value associated with keyFunc");

// getting the values
myMap.get(keyString);  // "value associated with 'a string'");
myMap.get(keyObj);     // "value associated with keyObj"
myMap.get(keyFunc);    // "value associated with keyFunc"

myMap.get("a string"); // "value associated with 'a string'");
                       // because keyString === 'a string'
myMap.get({});           // undefined, because keyObj !== {}
myMap.get(function() {}) // undefined, because keyFun !== function () {}

NaN can also be used as a key. Even though every NaN is not equal to itself (NaN !== NaN is true), the following example works, because NaNs are indistinguishable from each other:

var myMap = new Map();
myMap.set(NaN, "not a number");

myMap.get(NaN); // "not a number"

var otherNaN = Number("foo");
myMap.get(otherNaN); // "not a number"

Also note that JavaScript has two zero values, +0 and -0. These two zero values are treated as different keys in Maps:

var myMap = new Map();
myMap.set(0, "positive zero");
myMap.set(-0, "negative zero");

0 === -0; // true

myMap.get(-0); // "negative zero"
myMap.get(0);  // "positive zero"

Revision Source

<p>{{ fx_minversion_header("12") }}</p>
<p>{{ Non-standard_header() }}</p>
<h3>Introduction</h3>
<p><code>Map</code> objects are simple key/value maps. Keys can be primitive values or object references.</p>
<p>Keys are considered equal if their value is indistinguishable.</p>
<h2>API</h2>
<table class="standard-table"> <tbody> <tr> <th>Method</th> <th>Description</th> </tr> <tr> <td><code>myMap.get(key)</code></td> <td>Returns the value associated to the <code>key</code>, or <code>undefined</code> if there is none.</td> </tr> <tr> <td><code>myMap.set(key, value)</code></td> <td>Sets the value for the <code>key</code>  in <code>myMap</code>. Returns <code>undefined</code>.</td> </tr> <tr> <td><code>myMap.has(key)</code></td> <td>Returns a boolean asserting whether a value has been associated to the <code>key</code> in <code>myMap</code> or not</td> </tr> <tr> <td><code>myMap.delete(key)</code></td> <td>Removes any value associated to the <code>key</code>. After such a call, <code>myMap.has(key)</code> will return <code>false</code>.</td> </tr> </tbody>
</table>
<h2>Examples</h2>
<pre class="brush: js">var myMap = new Map();

var keyObj = {},
    keyFunc = function () {},
    keyString = "a string";

// setting the values
myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, "value associated with keyObj");
myMap.set(keyFunc, "value associated with keyFunc");

// getting the values
myMap.get(keyString);  // "value associated with 'a string'");
myMap.get(keyObj);     // "value associated with keyObj"
myMap.get(keyFunc);    // "value associated with keyFunc"

myMap.get("a string"); // "value associated with 'a string'");
                       // because keyString === 'a string'
myMap.get({});           // undefined, because keyObj !== {}
myMap.get(function() {}) // undefined, because keyFun !== function () {}
</pre>
<p><code>NaN</code> can also be used as a key. Even though every <code>NaN</code> is not equal to itself (<code>NaN !== NaN</code> is true), the following example works, because <code>NaN</code>s are indistinguishable from each other:</p>
<pre class="brush: js">var myMap = new Map();
myMap.set(NaN, "not a number");

myMap.get(NaN); // "not a number"

var otherNaN = Number("foo");
myMap.get(otherNaN); // "not a number"
</pre>
<p>Also note that JavaScript has two zero values, +0 and -0. These two zero values are treated as different keys in <code>Map</code>s:</p>
<pre>var myMap = new Map();
myMap.set(0, "positive zero");
myMap.set(-0, "negative zero");

0 === -0; // true

myMap.get(-0); // "negative zero"
myMap.get(0);  // "positive zero"</pre>
Revert to this revision