How to check JavaScript map size? [SOLVED]


JavaScript

Introduction

The Map object is a simple key/value map. Any value (objects and primitive values) may be used as either a key or a value. The Map object is a new object type introduced in JavaScript 1.6.

A Map is an associative array, meaning a set of key/value pairs. A key can be any value used as an index, including a string, number, or object. A value can be any JavaScript value (including another object).

In this article, we will talk about JavaScript Map objects and how to check a Map size.

 

Understanding JavaScript Map

We can create a Map with the new operator:

let myMap = new Map();

Or it can be created from an array of key/value pairs:

var myMap = new Map([
    ["key1", "value1"],
    ["key2", "value2"],
]);

Once a map is created, you can add, remove, and lookup key/value pairs with the following methods:

myMap.set("key", "value");
myMap.get("key");
myMap.has("key");
myMap.delete("key");

You can also iterate over the keys or values in a map with the following methods:

myMap.keys();
myMap.values();
myMap.entries();

Each method returns an iterator that can loop over the keys, values, or key/value pairs in the map. Here is a simple example that creates a map, adds some key/value pairs, and then iterates over the keys and values:

var myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", "value2");

for (var key of myMap.keys()) {
    console.log(key);
}
for (var value of myMap.values()) {
    console.log(value);
}
for (var [key, value] of myMap.entries()) {
    console.log(key + " = " + value);
}

Output

key1
key2
value1
value2
key1 = value1
key2 = value2

Now that we understand about Maps, how to get the size of a JavaScript Map object.

 

Check JavaScript Map Size

To obtain a JavaScript Map size, we can make use of the size property. The return value is the number of elements present within the Map object.

Here is a code showing us how to check the size of a JavaScript Map

var myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", "value2");
myMap.set("key3", "value3");

console.log(myMap.size);

Output

3

 

Summary

JavaScript Map object allows us to store key/value pairs, and to obtain their size, we can make use of the size property which returns the number of elements present within the Map object.

 

References

Map - JavaScript | MDN (mozilla.org)
Map.prototype.size - JavaScript | MDN (mozilla.org)

 

Views: 7
Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment