Data

Map

JavaScript

 

Defining Map

ECMAScript 5
var obj1 = {
  name: "Sam", 
  age: 21
};

var obj2 = new Object();

In JavaScript ES5, the closest native type to HashMaps are Objects.

You can use {} syntax to create objects literally.

 

Getting Key

ECMAScript 5
var obj = {name: "Sam", age: 21};

var name1 = obj.name;     // "Sam"
var name2 = obj["name"];  // "Sam"

In JavaScript, you can access a property (key) and get its value in 2 ways:

  • using Dot notation
  • using Bracket notation

If the key has space in it, then you need to use the Bracket notation to access that key.

 

Setting Key/Value

ECMAScript 5
var obj = {};

obj.name = "Sam";    // obj -> {name:"Sam"}
obj["name"] = "Sam"; // obj -> {name:"Sam"}

Setting a property (key) on an object can similarly be done using Dot or Bracket notation.

 

Length

ECMAScript 5
// No native support in ES5

Unfortunately Objects in JavaScript does not have a length property or method.

Since we're using objects as Maps here (ES5), then we need to mimic and implement the length ourselves.

 

Deleting Key

ECMAScript 5
var obj = {name: "Sam", age: 21};

delete obj.name;   // obj -> {age:21}
delete obj["age"]; // obj -> {}

The delete keyword deletes a key (and its value) from an Object.

After running the code, obj will be empty Object.

 

Listing Keys

ECMAScript 5
var obj = {name: "Sam", age: 21};

var keys = Object.keys(obj);   // ["name","age"]

The Object class keys() method, returns an array of object properties.

 

Listing Values

ECMAScript 5
var obj = {name: "Sam", age: 21};

var values = Object.values(obj);   // ["Sam",21]

The Object class values() method, returns an array of object property values.

 

Defining Map

ECMAScript 2015
var map = new Map();

In JavaScript ES6, a native object Map introduced which supports expected Map interface.

Just like any other data stores in JavaScript, key and value types are not getting fixed on declaration, so you can use differnt types for each element.

map.set('a', 1);
map.set(1, 'a');
 

Getting Key

ECMAScript 2015
var map = new Map();

map.set("one", 1);
map.set("two", 2);

var one = map.get("one");   // 1

The get() method returns a value associated with a key.

 

Setting Key/Value

ECMAScript 2015
var map = new Map();

map.set("one", 1);  // map -> {"one":1}
map.set("two", 2);  // map -> {"one":1, "two":2}

The set() method sets a key or updates an existing key with a new value.

 

Length

ECMAScript 2015
var map = new Map();

map.set("one", 1);
map.set("two", 2);

var length = map.size;  // 2

The size property returns a number of keys in a Map.

 

Deleting Key

ECMAScript 2015
var map = new Map();

map.set("one", 1);
map.set("two", 2);

map.delete("one");  // map -> {"two":2}

The delete() method deletes a key (and its value) from a Map.

If the key exists, it returns true otherwise false.

 

Listing Keys

ECMAScript 2015
var map = new Map();

map.set("one", 1);
map.set("two", 2);

var keysItr = map.keys();    // MapIterator

The keys() method returns an Iterator object for Map keys.

 

Listing Values

ECMAScript 2015
var map = new Map();

map.set("one", 1);
map.set("two", 2);

var valuesItr = map.values(); // MapIterator

The values() method returns an Iterator object for Map values.