| by Arround The Web | No comments

How to Get the Length of a Map in JavaScript

While programming in JavaScript, there can be a requirement to limit the size of a particular map so that its access can be made convenient. For instance, adjusting the length in such a way that access to each of the key-value pairs becomes feasible. In such case scenarios, getting the length of a map in JavaScript is of great aid in managing the data and memory effectively.

This article will demonstrate the approaches to get the length of a map in JavaScript.

How to Get the Map’s Length Using JavaScript?

A “map” holds key-value pairs in which there is no limitation upon the data type of keys. The “size” property can be utilized to find the length of a map in JavaScript. This property gives the number of elements within a map. More specifically, it will be utilized here to calculate the length of the map by simply referring to the created map.

Syntax

x.size

 
In the above-given syntax:

    • x” refers to the map to be computed for the size.

Example 1: Calculate the Length/Size of Map

In this particular example, the length of the created map will be computed by simply associating the “size” property with it:

<script type="text/javascript">
let mapSize = new Map();
mapSize.set('id', 1);
mapSize.set('name', 'Harry');
mapSize.set('age', 23);
console.log("The length of the map is:", mapSize.size);
</script>

 
In the above code snippet:

    • Firstly, create a new map object via the “new” keyword and the “Map()” constructor, respectively.
    • Now, apply the “set()” method to set the stated values for the keys in a map. The orientation in the map is in the form of “key-value” pairs.
    • Lastly, associate the “size” property with the created map “mapSize” to return the length of the map.

Output


 

In the above output, it can be observed that the length of the map is identical to the number of set values in the map.

Example 2: Calculate the Length/Size of Map Based on Condition

This example can be implemented to compute the map’s length based on the condition applied to a specific “key” in a map.

Let’s go through the following example:

<script type="text/javascript">
let mapSize = new Map();
mapSize.set('id', 1);
mapSize.set('name', 'Harry');
if (mapSize.has("id")){
console.log("The length of the map is:", mapSize.size - 1)
}
else{
console.log("The length of the map is:", mapSize.size)
}
</script>

 
Implement the following steps in the above code snippet:

    • Recall the discussed steps in the previous example for creating a new map object and setting the values for the stated “keys”.
    • After that, apply the “has()” method to locate the specified key within the map.
    • Upon the satisfied condition, apply the “size” property such that “1” is subtracted from the map’s computed length.
    • In the other scenario, the “else” condition will execute, referring to the default length.

Output


 

It is evident in the above output that the particular “key” is included in the map, and hence, the “if” condition is executed.

Conclusion

The “size” property can be utilized to get the length of a map directly or by placing an exception upon the map key in JavaScript. This property can simply be applied to the created map to count the number of elements in a map and return the corresponding length. It can also be applied based on a particular condition upon the map keys. This tutorial demonstrates how to fetch the map’s length in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply