mozilla

Revision 19283 of Map

  • Revision slug: JavaScript/Reference/Global_Objects/Map
  • Revision title: Map
  • Revision id: 19283
  • Created:
  • Creator: Marcoos
  • Is current revision? No
  • Comment 2 words added, 2 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.

Example

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 () {}

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>Example</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>
Revert to this revision