JavaScript Object Maps

Maps are data structures that allow for the storage of key-value pairs, where each key is unique.

The primitive data types like strings and numbers are commonly used as keys in maps, objects can also serve as keys, enabling developers to create complex mappings between objects and their associated values.

Let's delve into object maps in JavaScript, exploring how they are created, utilized, and their advantages over traditional object property access.

Creating Object Maps

Bbject maps are typically implemented using the Map data structure provided by the language.

This data structure allows for the storage of key-valuepairs where the keys can be objects.

Let's consider an example where we create an object map to store information about books, with each book object serving as the key and its corresponding author as the value.


let bookMap = new Map();

// Adding key-value pairs to the map
let book1 = {title: "The Great Gatsby"};
let book2 = {title: "To Kill a Mockingbird"};
  
bookMap.set(book1, "F. Scott Fitzgerald");
bookMap.set(book2, "Harper Lee");

            

Accessing Values from Object Maps

Once key-value pairs are added to the object map, values can be accessed using the corresponding keys.

This is achieved using the get()method provided by the Mapdata structure.


let authorOfBook1 = bookMap.get(book1);

console.log(authorOfBook1);// Output: F. Scott Fitzgerald
            

Example

let bookMap = new Map();

// Adding key-value pairs to the map
let book1 = {title: "The Great Gatsby"};
let book2 = {title: "To Kill a Mockingbird"};
  
bookMap.set(book1, "F. Scott Fitzgerald");
bookMap.set(book2, "Harper Lee");

let authorOfBook1 = bookMap.get(book1);

console.log(authorOfBook1);// Output: F. Scott Fitzgerald
  

Checking for Key Existence

It's often necessary to check whether a specific key exists within an object map.

This can be accomplished using the has() method provided by the Map data structure.


if (bookMap.has(book2)){ 
  console.log("Book 2 exists in the map");
}else {
  console.log("Book 2 does not exist in the map");
}
            

Example

let bookMap = new Map();

// Adding key-value pairs to the map
let book1 = {title: "The Great Gatsby"};
let book2 = {title: "To Kill a Mockingbird"};
  
bookMap.set(book1, "F. Scott Fitzgerald");
bookMap.set(book2, "Harper Lee");

if (bookMap.has(book2)){ 
  console.log("Book 2 exists in the map");
}else {
  console.log("Book 2 does not exist in the map");
}
  

Iterating Over Object Maps

Iterating over object maps allows us to perform operations on each key-value pair stored within the map.

This can be achieved using methods like forEach()or the for...ofloops.

// Using forEach()
bookMap.forEach((author, book) => { 
  console.log(`${book.title} was written by ${author}`);
})

// Using for...of loop
for(const [book, author] of bookMap){ 
  console.log(`${book.title} was written by ${author}`);
}
          

Example

let bookMap = new Map();

// Adding key-value pairs to the map
let book1 = {title: "The Great Gatsby"};
let book2 = {title: "To Kill a Mockingbird"};
  
bookMap.set(book1, "F. Scott Fitzgerald");
bookMap.set(book2, "Harper Lee");

// Using forEach()
bookMap.forEach((author, book) => { 
  console.log(`${book.title} was written by ${author}`);
})

// Using for...of loop
for(const [book, author] of bookMap){ 
  console.log(`${book.title} was written by ${author}`);
}
  

Removing Key-Value Pairs from Object Maps

Removing key-value pairs from object maps is a common operation and can be achieved using the delete()method provided by the Mapdata structure.


bookMap.delete(book1);

// Example usage
for(const [book, author] of bookMap){ 
  console.log(`${book.title} was written by ${author}`);
}
            

Example

let bookMap = new Map();

// Adding key-value pairs to the map
let book1 = {title: "The Great Gatsby"};
let book2 = {title: "To Kill a Mockingbird"};
  
bookMap.set(book1, "F. Scott Fitzgerald");
bookMap.set(book2, "Harper Lee");

bookMap.delete(book1);

// Example usage
for(const [book, author] of bookMap){ 
  console.log(`${book.title} was written by ${author}`);
}
  

Object maps in JavaScript provide a powerful way to establish mappings between objects and their associated values.

By leveraging the Mapdata structure, developers can efficiently store, access, and manipulate key-valuepairs where objects serve as keys.