Map and Set are the newly introduced collection types in JavaScript that are used for storing multiple values in a single variable. Both of them have different use cases which make them unique in their own way. While Map is used for storing data in the form of Key:Value pairs, Set is used for storing unique data items without any keys, just indices. Show
Scope of Article
What is Map in JavaScript?Map is one of the two newly introduced(in ES6) data structures (of collection type) in JavaScript that is used for storing the data in the form of key: value pairs, inside a single variable. Wait we already have objects to do that right? Yes, We have Objects to store the data in the form of key:value pairs but there are certain limitations in Objects that are not in Maps. Hence, Maps have more functionalities compared to objects in JavaScript. We will discuss more about Map vs objects later in this article, so stay tuned. Let us see how to create and initialize Maps in JavaScript. How to Create and Initialize a Map in JavaScript?The process of creating and initializing Maps in JavaScript is the same as that for the Objects in JavaScript. The syntax is: Syntax:
Note: The Time Complexity for insertion, searching and deletion of entries or elements in Map and Set in JavaScript is O(log(n)). Inserting/Adding Values to a MapMap in JavaScript has a predefined method called the set() method that can be used to add new values or update the existing data of the Map. Example:
Note: You can learn more about destructuring here Map can also use objects as keysThe keys of Map in JavaScript are amazing as we can specify any kind of value(such as boolean, number, function, or even object) as a key. But as far as Objects are concerned, they lack this functionality. For example: 0 Map vs. Object: When should you use them?Both Map, as well as Object, are used to store a collection of keyed elements or keyed data inside a single variable. The object is one of the oldest members of JavaScript while Map was introduced in ES6 which means that Map in JavaScript is the newly introduced collection type. Let us see some advantages of Map over Objects. 1. Index and orderObjects are unindexed which means that they are not always in a particular order of insertion while Maps are indexed hence they are always in a particular order of insertion. We know about Arrays in JavaScript which also follows the same indexing and order pattern. Hence, just for the understanding purpose, we may say that Maps are the combination of Objects and Arrays as in Maps we have the power of key: value pair as seen in Objects and the power of indexing and order of insertion as seen in Arrays. 2. Simple and Easy iterationIteration in Objects is not as simple as it is in Maps because Maps have some in-built methods like the forEach() method for iteration which makes it a cakewalk to iterate over a Map; its entries, its keys, and its values also. 3. The Amazing KeysMap in JavaScript can have keys that belong to any data type be it number, boolean, a function, or even an object and this functionality is not there in the Objects. 4. The size of MapThe size property comes in-built in Maps which is an added advantage of using Maps over Objects as Objects have no such in-built way of getting their size. Now the question arises which collection type to use when? There are some advantages of Objects also like they are great when dealing with JSON.parse() and JSON.stringify() functions which make it easy to work with JSON. It should be noted that JSON is a widely used data format that deals with many REST APIs. So after going through the above-mentioned points, it can be easier for you to select which one best suits best for your requirement. Object.entries(): Creating a Map from an ObjectThe Map can also be created with the pre-specified [Key, Value] paired Array of Arrays. We just need to pass this Array inside the Map constructor while creating a new Map. Example: 2 How to Convert a Map into an Array in JavaScript?There can be situations when we want to have an array of all the keys of a map or all the values of a map. To get an array of the keys of Map we can use the Map.keys() method and to get an array of values of Map we can use the Map.Values() method with the spread operator which spreads the given data into an array. Example: 8 Note: You can learn more about the spread operator here WeakMaps in JavaScriptIn addition to Map in JavaScript, we have something called WeakMaps which are nothing but Maps with limited functionalities. As we create a Map, the same way we create a WeakMap, we just need to replace the Map() constructor with the WeakMap() constructor. Syntax: 0 Map Properties and MethodsHere is a table that consists of all the important Methods and Properties of the Map with their details and return value. Method/PropertyDetailsReturn Valueadd(value)It adds a new element to a SetSet Objectdelete(value)It removes the element from a Set using its keyBooleanvalues()It returns all the keys present in a SetSetIterator objectkeys()It returns all the keys present in a Set (same as values())SetIterator objectentries()It returns all the entries from the Set (same as values())SetIterator objectclear()It clears the Map by removing all the items from a Set-has(key)It checks if the element is present in the Set or not using a keyBooleanforEach()It iterates over the Set-sizeIt returns the number of entries present in a SetNumber What is the use of map and set in JavaScript?Map – is a collection of keyed values. Methods and properties: new Map([iterable]) – creates the map, with optional iterable (e.g. array) of [key,value] pairs for initialization. map.set(key, value) – stores the value by the key, returns the map itself.
What is difference between map and set?The main difference between Set and Map is that Set contains only data elements, and the Map contains the data in the key-value pair, so Map contains key and its value.
What is the difference between map and set in es6?The difference is that a map has a key-value pair and two dimensions. It is possible to convert both 2D arrays and arrays to sets. To summarise, Map is used for key-value pair collections, while Set is used for unique value collections.
What is the map method used for?The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
|