The Wayback Machine - https://web.archive.org/web/20240623063115/https://www.geeksforgeeks.org/javascript-map/
Open In App

JavaScript Map

Last Updated : 29 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

map() creates a new array from calling a function for every array element. It does not execute the function for empty elements or change the original array. JavaScript Map is a collection of key-value pairs, enabling efficient data retrieval and manipulation.

On iterating a map object returns the key, and value pair in the same order as inserted. Map() constructor is used to create Map in JavaScript.

JavaScript Map has a property that represents the size of the map.

Example:

Input:
let map1 = new Map([
[1 , 10], [2 , 20] ,
[3, 30],[4, 40]
]);

console.log("Map1: ");
console.log(map1);
Output:
// Map1:
// Map(4) { 1 => 10, 2 => 20, 3 => 30, 4 => 40 }

Steps to Create a Map

  • Passing an Array to new Map()
  • Create a Map and use Map.set()

Examples of JavaScript Map

new Map()

In this we use new Map() constructor,

Example: In this example, a Map named prices is created to associate product names with their respective prices, allowing for efficient retrieval and management of price information.

// Creating a Map for product prices
const prices = new Map([
["Laptop", 1000],
["Smartphone", 800],
["Tablet", 400]
]);

Map.set()

You can add elements to a Map with the set() method.

Example: In this example, the Map.set() method is employed to add product prices to the Map named prices.

// Creating a Map for product prices
const prices = new Map();
// Using Map.set() to add product prices
prices.set('Laptop', 1000);
prices.set('Smartphone', 800);
// The Map now contains { 'Laptop' => 1000, 'Smartphone' => 800 }

Example 1: In this example, we will create a basic map object

Javascript
let map1 = new Map([
    [1, 2],
    [2, 3],
    [4, 5]
]);

console.log("Map1");
console.log(map1);

let map2 = new Map([
    ["firstname", "sumit"],
    ["lastname", "ghosh"],
    ["website", "geeksforgeeks"]
]);

console.log("Map2");
console.log(map2);

Output
Map1
Map(3) { 1 => 2, 2 => 3, 4 => 5 }
Map2
Map(3) {
  'firstname' => 'sumit',
  'lastname' => 'ghosh',
  'website' => 'geeksforgeeks'
}

Example 2: This example adds elements to the map using set() method.

Javascript
let map1 = new Map();
map1.set("FirstName", "Shobhit");
map1.set("LastName", "Sharma");
map1.set("website", "GeeksforGeeks");

console.log(map1);

Output
Map(3) {
  'FirstName' => 'Shobhit',
  'LastName' => 'Sharma',
  'website' => 'GeeksforGeeks'
}

Example 3: This example explains the use of Map methods like has(), get(), delete(), and clear().

Javascript
let map1 = new Map();

map1.set("first name", "sumit");
map1.set("last name", "ghosh");
map1.set("website", "geeksforgeeks")
    .set("friend 1","gourav")
    .set("friend 2","sourav");

console.log(map1);
    
console.log("map1 has website ? "+ 
                    map1.has("website"));

console.log("map1 has friend 3 ? " + 
                    map1.has("friend 3"));

console.log("get value for key website "+
                    map1.get("website"));

console.log("get value for key friend 3 "+
                    map1.get("friend 3"));
console.log("delete element with key website " 
                    + map1.delete("website"));
    
console.log("map1 has website ? "+ 
                    map1.has("website"));

console.log("delete element with key website " +
                    map1.delete("friend 3"));

map1.clear();

console.log(map1);

Output
Map(5) {
  'first name' => 'sumit',
  'last name' => 'ghosh',
  'website' => 'geeksforgeeks',
  'friend 1' => 'gourav',
  'friend 2' => 'sourav'
}
map1 has website ? true
map1 has friend 3 ? false
get...

Advantages of Map:

Map object provided by ES6. A key of a Map may occur once, which will be unique in the map’s collection. There are slight advantages to using a map rather than an object.

  • Accidental Keys & Security: No default keys are stored, only contain what’s explicitly put into them. Because of that, it’s safe to use.
  • Key Types & Order: It can be any value as a key function, object anything. And the order is straightforward way in the order of entry insertion.
  • Size: Because of the size property a map can be easily retrieved.
  • Performance: Any operation can be performed on math so easily in a better way.
  • Serialization and parsing: We can create our own serialization and parsing support for Map by using JSON.stringify() and JSON.parse() methods.

Supported Browsers:



Previous Article
Next Article

Similar Reads

How to map array values without using map method in JavaScript ?
Array elements can be mapped by using looping methods in JavaScript. The map() method creates a new array with the results of the output of a function called for each array element. This can also be implemented using a for loop in JavaScript. Approach 1: For this, we can create two arrays, in which one array contains the array elements that are to
2 min read
PHP | Ds\Map map() Function
The Ds\Map::map() function of the Map class in PHP is used to apply a callback function to a Map object. This returns the result of applying the callback function to each value present on the map. The function does not update the values in the original map, instead, it just returns the result of the updates without affecting the original values. Sy
2 min read
What is image map & how to map the image in HTML ?
In this article, we will discuss an image map in HTML, along with understanding its implementation through the examples. An Image Map refers to the clickable image having a clickable area that can be used to navigate to the various link to other web pages or the specific section of the same web page. The <map> tag can be used to define the im
2 min read
Create a Map with Google Map Api using React-Native
In this project, we'll explore how to integrate Google Maps into a React Native application. We'll create a dynamic map that allows users to view their current location and interact with markers on the map. This project aims to provide a practical guide for developers looking to incorporate maps into their mobile applications using React Native. Ou
3 min read
Implement polyfill for Array.prototype.map() method in JavaScript
In this article, we will learn how to implement a polyfill for an Array.prototype.map() method in JavaScript. What is a polyfill? A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or because the new version of the br
3 min read
JavaScript Array map() Method
JavaScript Array map() Method method creates a new array with the results of a called function for every array element. This function calls the argument function once for each element of the given array in order. Syntax: // Arrow function map((element) => { /* … */ }) map((element, index) => { /* … */ }) map((element, index, array) => { /*
3 min read
JavaScript Map.prototype[@@iterator]() Method
Map[@@iterator]( ) method is used to make Map iterable. Map[@@iterator]( ) method returns iterator object which iterates over all code points of Map. Map[@@iterator]( ) is built-in property of Map. We can use this method by creating Map iterator. We can make Map iterator by calling the @@iterator property. In place of @@iterator, we can use Symbol.
2 min read
JavaScript Map clear() Method
JavaScript Map.clear() method is used for the removal of all the elements from a map and making it empty. It removes all the [key, value] from the map. No arguments are required to be sent as parameters to the Map.clear() method and it returns an undefined return value. Syntax: mapObj.clear() Parameters: No parameters are required in the Map.clear(
2 min read
JavaScript Map get() Method
The Map.get() method in JavaScript is used for returning a specific element among all the elements which are present in a map. The Map.get() method takes the key of the element to be returned as an argument and returns the element which is associated with the specified key passed as an argument. If the key passed as an argument is not present in th
3 min read
JavaScript typedArray.map() Method
The typedArray.map() is an inbuilt function in JavaScript which is used to create a new typedArray with the result of a provided function on each element of the given typedArray. Syntax: typedArray.map(callback) Parameters: It accepts a parameter callback function which accept some parameter which are specified below- currentValue: It is the curren
1 min read