4 Easy Ways to Find Size of the Map in JavaScript

Map Size in JavaScript

In this article, we’ll see how to find the size of a map in javascript.

What is a map?

It is an object, we can store data as key-value pairs using map in javascript. The key you provided must be a unique name, so it doesn’t accept duplicate keys and values ​​can be repeated. We can also store key-value pairs as objects or other primitive data types. To create a map object,

let map1 = new Map()

We can add the key-value pairs using the set() method, like:

map1.set('key1', 'value1');
map1.set('key2', 'value2');
map1.set('key3', 'value3');

console.log(map1)

Output:

map size in javascript

4 Ways to find the size of the map

1. Using size() method:

The most straightforward way to get the size of a Map is by accessing its size property. The size property returns an integer representing the number of elements in the Map.

const myMap = new Map();
myMap.set('key1', 'value1');
myMap.set('key2', 'value2');
myMap.set('key3', 'value3');

const mapSize1 = myMap.size;
console.log(mapSize1); // Output: 3

2. Using for loop

We can also find the size of a Map is by iterating using for loop in javascript through its key-value pairs and counting them manually. This method can be useful if you need to perform additional operations while traversing the Map.

let mapSize2 = 0;
for (const [key, value] of myMap) {
  mapSize2++;
}
console.log(mapSize2); // Output: 3

3. Using the Spread Operator

You can use this spread operator method to find the size of two or more maps. We can use the spread operator (…) to convert the Map’s entries to an array and then, use the length property to get the size of the Map. So, the syntax is :

[...Map1, ...Map2, ...Map3, ...].length;

In the below example, we have found the size of two maps “myMap1” and “myMap2” at the same time by using the spread operator.

const myMap1 = new Map();
myMap1.set('key1', 'value1');
myMap1.set('key2', 'value2');

const myMap2 = new Map();
myMap2.set('key1', 'value1');
myMap2.set('key2', 'value2');

const mapSize4 = [...myMap1, ...myMap2].length;
console.log(mapSize4); // Output: 4

4. Using reduce()

In this method, first, we have to convert the Map to an array of key-value pairs using the Map.prototype.entries() method. Then we have to use the reduce() method on the array to calculate the size as in the below example.

const myMap = new Map();
myMap.set('key1', 'value1');
myMap.set('key2', 'value2');
myMap.set('key3', 'value3');

// Convert the Map to an array of key-value pairs using the entries() method
const mapEntries = Array.from(myMap.entries());

// Use reduce() to calculate the size of the Map
const mapSize5 = mapEntries.reduce((size, [key, value]) => size + 1, 0);

console.log(mapSize5); // Output: 3

In this example, the mapEntries the array will be [["key1", "value1"], ["key2", "value2"], ["key3", "value3"]], and the mapSize will be calculated by using reduce() with an initial value of 0, and incrementing the size by 1 for each key-value pair in the array.

READ ALSO  Simple steps to Get Map Values in JavaScript using for of loop

Conclusion

So, You can use these 4 methods as per your requirement

Leave a Reply