DEV Community

Cover image for JavaScript:  Using the Map Object
Monica Gerard
Monica Gerard

Posted on

JavaScript: Using the Map Object

The Map object in JavaScript bears a strong resemblance to the standard Object that uses key/value pairs. However, it comes with its own set of methods that makes key/value pairs a breeze to work with, and in some cases provides distinct advantages over standard objects.

Here's a quick summary of the differences between the Map object and an Object object:

Image description

Map Methods

new Map()

Create a map object by passing an array of key/value pairs to the constructor:



let pets = new Map([
  ["dog", "Fido"],
  ["cat", "Milo"],
  ["bird", "Polly"]
])
console.log(pets)


Enter fullscreen mode Exit fullscreen mode


Map(3) { 'dog' => 'Fido', 'cat' => 'Milo', 'bird' => 'Polly' }


Enter fullscreen mode Exit fullscreen mode

.set()

You can add new key/value pairs to a Map object with .set():



pets.set("turtle", "Yertle")
console.log(pets)


Enter fullscreen mode Exit fullscreen mode


Map(4) {
  'dog' => 'Fido',    
  'cat' => 'Milo',    
  'bird' => 'Polly',  
  'turtle' => 'Yertle'
}


Enter fullscreen mode Exit fullscreen mode

.get()

Use the .get() method to use the key to get a value:



let myTurtle = pets.get("turtle")
console.log(myTurtle)


Enter fullscreen mode Exit fullscreen mode


Yertle


Enter fullscreen mode Exit fullscreen mode

.size

The size property will give you the number of key/value pairs in your Map object:



console.log(pets.size)


Enter fullscreen mode Exit fullscreen mode


4


Enter fullscreen mode Exit fullscreen mode

.has()

The .has() method will indicate whether or not a key exists in the Map object



console.log(pets)
console.log(pets.has("cat"))
console.log(pets.has("armadillo"))


Enter fullscreen mode Exit fullscreen mode


Map(4) {
  'dog' => 'Fido',
  'cat' => 'Milo',
  'bird' => 'Polly',
  'turtle' => 'Yertle'
}
true
false


Enter fullscreen mode Exit fullscreen mode

.forEach()

.forEach() will invoke a callback function for each key/value pair:



let petList = ""
pets.forEach( (value, key) => {
    petList += key + ": " + value + "\n"
})
console.log(petList)


Enter fullscreen mode Exit fullscreen mode


dog: Fido
cat: Milo
bird: Polly
turtle: Yertle


Enter fullscreen mode Exit fullscreen mode

.entries()

.entries() returns an iterator object that includes the key/value pairs:



let petList = ""
for (const pet of pets.entries()) {
  petList += pet + "\n"
}
console.log(pets.entries())
console.log(petList)


Enter fullscreen mode Exit fullscreen mode


[Map Entries] {
  [ 'dog', 'Fido' ],    
  [ 'cat', 'Milo' ],    
  [ 'bird', 'Polly' ],  
  [ 'turtle', 'Yertle' ]
}
dog,Fido
cat,Milo
bird,Polly
turtle,Yertle


Enter fullscreen mode Exit fullscreen mode

.keys()

.keys() returns an iterator object that contains the Map object's keys:



console.log(pets.keys())

let petsKeys = ""
for (const pet of pets.keys()) {
  petsKeys += pet + "\n"
}
console.log(petsKeys)


Enter fullscreen mode Exit fullscreen mode


[Map Iterator] { 'dog', 'cat', 'bird', 'turtle' }
dog
cat   
bird  
turtle


Enter fullscreen mode Exit fullscreen mode

.values()

.values() returns an iterator object that contains the Map object's values:



console.log(pets.values())

let petsValues = ""
for (const pet of pets.values()) {
  petsValues += pet + "\n"
}
console.log(petsValues)


Enter fullscreen mode Exit fullscreen mode


[Map Iterator] { 'Fido', 'Milo', 'Polly', 'Yertle' }
Fido
Milo
Polly
Yertle


Enter fullscreen mode Exit fullscreen mode

.delete()

.delete() removes an element from a Map object:



console.log(pets.entries())
pets.delete("bird")
console.log(pets.entries())


Enter fullscreen mode Exit fullscreen mode


[Map Entries] {
  [ 'dog', 'Fido' ],    
  [ 'cat', 'Milo' ],    
  [ 'bird', 'Polly' ],  
  [ 'turtle', 'Yertle' ]
}
[Map Entries] {
  [ 'dog', 'Fido' ],    
  [ 'cat', 'Milo' ],    
  [ 'turtle', 'Yertle' ]


Enter fullscreen mode Exit fullscreen mode

.clear()

And finally, .clear() removes all the elements from the Map object:



console.log(pets)
pets.clear()
console.log(pets)


Enter fullscreen mode Exit fullscreen mode


Map(4) {
  'dog' => 'Fido',    
  'cat' => 'Milo',    
  'bird' => 'Polly',  
  'turtle' => 'Yertle'
}
Map(0) {}


Enter fullscreen mode Exit fullscreen mode

With all these useful methods, the Map object provides an efficient and intuitive interface for working with key/value pairs. Consider implementing one in place of the standard object in your next application!

Happy Coding!

Top comments (0)