Map
We'll cover the following...
ES6 introduces a few new ways to store data. Up until now we have had Objects and Arrays to store information. In ES6 we now have Map, WeakMap, Set and WeakSet. These Objects provide a new way to store data in a collection of either key value pairs, or collection of values.
Why new data structures
Most languages will have data structures that are purely for data; Ruby has Hashes, Python has Dictionaries. The goal of these structures is to create a simple data store. In JavaScript we have always used Objects to do this. Objects can be simple key value pairs, or include more features, such as methods.
However with Objects in JavaScript comes lots of added data that we might not want. When you create a simple object:
const person = {};
This will create an object that has a prototype chain, it comes with a __proto__
object that contains inherited methods. We can create Objects that don’t have this chain by using the Object.create()
method to create a simple empty object.
const person = Object.create(null);
Doing so will create an object with no methods or properties at all. Let’s look at how we can use Map
and Set
to work with data. This chapter will be about Map, and in the next chapter we will look at Set.
Map
In ES6 a Map is a key value object that we can instantiate to store data. Maps can use any value as a key in the object, and this includes other objects which we will see a little ...