How to use JavaScript Collection with Map

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Search for a command to run...

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
I am using this web tutorial to share useful information to those who have no ideas and wants to get solution how bing history clear then follow and get tips from here.
Traditional SSR is great, but it has a massive buffering problem. Let's look into the SSR Streaming and learn how it could be a game changer.

Learn how to build maintainable, reusable forms in React using design patterns like custom hooks, compound components, and context for scalable CRUD

Learn how JavaScript variables work, including let vs const, primitive and reference types, pass by value, and how JavaScript stores data in memory.

A beginner-friendly introduction to JavaScript covering what it is, how it works, and how to set up your JavaScript environment

Learn how the promises are handled internally using event loop. This concept is essentials to debug and work with async programming.

GreenRoots Blog - A Blog by Tapas Adhikary
186 posts
Educator | YouTuber | Building ReactPlay | Let's Connect
Unlike many other programming languages, JavaScript's way of handling data collection is mostly with objects and arrays(don't forget, technically array is also a type of object in JavaScript). A majority of the developers rely on these heavily for slicing and dicing the data into a suitable data structure.
In the pre-ES6 era, there were not many options to deal with collections of data. Using an array was a great way of achieving it. The combination of an array and object makes the data collection useful. But, there are some shortcomings with this:
size of it, or the flexibility of iterating through it.ES6(ECMAScript 2015) brought us two new data structures, Map and Set to make the data collection more flexible and useful. In this article, we will go over the Map data structure to learn how to use it in practice.
An explanation of the Set data structure will follow as a future article post.
A Map is a collection of key-value pairs where the key can be of any type. The Map remembers the original insertion order of the elements. It means the data from the Map will be retrieved in the same order that it was inserted.
If you notice closely, Map has characteristics of both Object and Array.
A new Map can be created as,
const map = new Map();
It returns an empty Map.
Map(0) {}
A point to note here. A newly created Map has no default keys. Whereas, if you create a JavaScript object to make it work like a Map, it would inherit properties from its prototype.
Another way of creating a Map is with initial values. Here we are creating a Map initializing with three key-value pairs,
const greenrootsBlog = new Map([
['name', 'greenroots'],
['type', 'blog'],
['writer', 'Tapas Adhikary'],
]);
It returns a Map with three elements,
Map(3)
{
"name" => "greenroots",
"type" => "blog",
"writer" => "Tapas Adhikary"
}
To add value to a Map, use the set(key, value) method. The set(key, value) method takes two parameters, key and value, where the key and value can be of any type, primitives(boolean, string, number etc) or an object.
// create a map
const map = new Map();
// Add values to the map
map.set('name', 'greenroots');
map.set('type', 'blog');
map.set('writer', 'Tapas Adhikary');
Output,
Map(3)
{
"name" => "greenroots",
"type" => "blog",
"writer" => "Tapas Adhikary"
}
Please note, if you use the same key to add values multiple times to a Map, it will always replace the last value.
// Add a different writer
map.set('writer', 'Someone else!');
Now the map output will be,
Map(3)
{
"name" => "greenroots",
"type" => "blog",
"writer" => "Someone else!"
}
You must have guessed it by now. Yeah, Map has a method called, get(key) for getting value from it by passing a key.
map.get('name');
map.get('type');
map.get('writer');
Please note, the get(key) method returns an undefined if a non-existing key is passed to it.
A prominent difference of an object and the Map is, Map keys can be of any type. Let us see it with examples.
// create a Map
const funMap = new Map();
funMap.set(360, 'My House Number'); // number key
funMap.set(true, 'I write blogs!'); // boolean key
let obj = {'name': 'tapas'}
funMap.set(obj, true); // object as key
console.log(funMap);
Output,
Map(3)
{
360 => "My House Number",
true => "I write blogs!",
{…} => true
}
Now,
funMap.get(360); // returns, 'My House Number'
funMap.get(obj); // returns, true
funMap.get('360'); // undefined
A regular JavaScript object always treats the keys as strings. Even when you pass them as other primitives or objects, it internally converts the keys to strings. Here is an example to understand it,
// Create an empty object
const funObj = {};
// add a property. Note, passing the key as a number.
funObj[360] = 'My House Number';
// It returns true.
// Because the number 360 got converted to a string '360' internally!
console.log(funObj[360] === funObj['360']);
The Map has built-in properties and methods that make it so powerful and flexible to use. Let's create a Map to explain them.
const map = new Map();
map.set('John', 34);
map.set('Alex', 15);
map.set('Buddy', 37);
Use the size property of the Map to know how many elements are in it.
console.log('size of the map is', map.size);
It reruns the number of elements in a Map. In this case, it will be 3.
Please note: Just like array's
length,sizeis also a property, not a method.
The method has(key) returns true if the Map has an element with the key.
console.log(map.has('John')); // returns, true
console.log(map.has('Tapas')); // returns, false
We can delete an element from the map using the delete(key) method.
map.delete('Buddy'); // removes the element with key, 'Buddy'.
Use the clear() method to remove all the elements at once from the Map.
// Clear the map by removing all the elements
map.clear();
map.size // It will return, 0
All the methods(except clear()) we have seen so far, is to deal with the key-value of a Map one-by-one. There are three useful methods to get all the keys, values, and key-value pairs respectively.
These methods return a MapIterator which is excellent because you can do a for-of or forEach loop directly on it.
First, create a Map,
const ageMap = new Map([
['Jack', 20],
['Alan', 34],
['Bill', 10],
['Sam', 9]
]);
console.log(ageMap.keys());
Output,
MapIterator {"Jack", "Alan", "Bill", "Sam"}
console.log(ageMap.values());
Output,
MapIterator {20, 34, 10, 9}
console.log(ageMap.entries());
Output,
MapIterator {"Jack" => 20, "Alan" => 34, "Bill" => 10, "Sam" => 9}
There are multiple ways of iterating over a Map. You can use, forEach or for-of loop to iterate over it.
// with forEach
ageMap.forEach((value, key) => {
console.log(`${key} is ${value} years old!`);
});
Note, the first argument is the value and the second is the key. The output is,
Jack is 20 years old!
Alan is 34 years old!
Bill is 10 years old!
Sam is 9 years old!
We can simply destructure the keys and values from the Map using the for-of loop.
for(const [key, value] of ageMap) {
console.log(`${key} is ${value} years old!`);
}
You may encounter a situation where you need to convert an object to a Map like structure. You can use the method, entries of Object to do that.
const address = {
'Tapas': 'Bangalore',
'James': 'Huston',
'Selva': 'Srilanka'
};
const addressMap = Object.entries(address);
If you want to do the reverse, you can use the method called, fromEntries.
Object.fromEntries(map)
There are a couple of ways to convert a map to an array.
Using Array.from(map)
const map = new Map();
map.set('milk', 200);
map.set("tea", 300);
map.set('coffee', 500);
console.log(Array.from(map));
Output,

Using the spread operator
We can use the spread operator as well to convert a Map to an array.
console.log([...map]);
Map has characteristics of both object and array. However, Map is more like an object than array due to the nature of storing data in the key-value format.
The similarity with the object ends here though. The Map is very different from the object in many other ways as we have seen so far. So, which one to use, when? How to take that call?
Use Map when
You are looking for flexibilities without relying on an external library like lodash. You may end up using a library like lodash because we do not find methods like, has(), values(), delete() or a property like, size with the object.
Map makes it easy for you by providing them by default.
Use Object when
JSON.parse() as, a Map cannot be parsed with it.Hope it was a useful explanation of the Map data structure in JavaScript. Give it a try if you are not using it already.
I shall be explaining another data structure called, Set in my next article. Please stay tuned.
You may also like other JavaScript related articles,
Follow me on twitter @tapasadhikary for more updates.