Skip to content

Map Object JavaScript | Basics

  • by

JavaScript Map object holds key-value pairs and remembers the original insertion order of the keys. A Map object holds key-value pairs where the keys can be any datatype.

To create a new Map, you use the following syntax:

let map = new Map([iterable]);

Map Object JavaScript

Simple example code creates a new Map object.

<!DOCTYPE html>
<html>
<body>
  <script >
    const map1 = new Map();

    map1.set('a', 1);
    map1.set('b', 2);
    map1.set('c', 3);

    console.log(map1)

  </script>
</body>
</html>

Output:

Map Object JavaScript

More examples

const map1 = new Map();

map1.set('a', 1);
map1.set('b', 2);
map1.set('c', 3);

console.log(map1.get('a')); // 1

map1.set('a', 97);

console.log(map1.get('a')); // 97

console.log(map1.size); // 3

map1.delete('b');

console.log(map1.size); // 2

Example map to the Object

var myObject = { 'a': 1, 'b': 2, 'c': 3 };

Object.keys(myObject).map(function(key, index) {
  myObject[key] *= 2;
});

console.log(myObject);

Output:

{
  "a": 2,
  "b": 4,
  "c": 6
}

Do comment if you have any doubts or suggestions on this JavaScript Map object basic topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading