How to create JavaScript Dictionary? [SOLVED]


JavaScript

Introduction

In JavaScript, a dictionary (also known as a map or associative array) is a data structure that is used to store key-value pairs. A dictionary allows you to retrieve a value by its corresponding key, and to add, remove, or update key-value pairs in the dictionary.

In this article, we will discuss how to create a dict in JavaScript using Objects and Maps.

 

Method-1: Use the Object constructor to create dict in JavaScript

To create a dictionary in JavaScript, you can use the Object constructor and add key-value pairs to the object using dot notation or bracket notation. For example:

// Using dot notation
let myDictOne = new Object();
myDictOne.key1 = "value1";
myDictOne.key2 = "value2";

console.log(myDictOne);

// Using bracket notation
let myDictTwo = new Object();
myDictTwo["key1"] = "value1";
myDictTwo["key2"] = "value2";

console.log(myDictTwo);

Output

{ key1: 'value1', key2: 'value2' }
{ key1: 'value1', key2: 'value2' }

In these examples, a new dictionary is created using the Object constructor, and two key-value pairs are added to the dictionary using dot notation or bracket notation.

Once you have created a dictionary and added some key-value pairs to it, you can access the values in the dictionary by their corresponding keys. For example:

let value1 = myDictTwo.key1;
let value2 = myDictTwo["key2"];

console.log(value1, value2);

Output

value1 value2

In this example, the values in the myDictTwo dictionary are accessed using dot notation and bracket notation. The value associated with the key1 key is assigned to the value1 variable, and the value associated with the key2 key is assigned to the value2 variable.

In addition to accessing values in a dictionary, you can also add, remove, or update key-value pairs in the dictionary. For example:

// Adding a new key-value pair
myDictTwo.key3 = "value3";

// Updating the value of an existing key
myDictTwo.key2 = "new value";

// Removing a key-value pair
delete myDictTwo.key1;

console.log(myDictTwo);

Output

{ key2: 'new value', key3: 'value3' }

In these examples, a new key-value pair is added to the myDictTwo dictionary, the value associated with an existing key is updated, and a key-value pair is removed from the dictionary.

 

Method-2: Use the Map class

In addition to using the Object constructor to create a dictionary in JavaScript, you can also use the Map class from the ECMAScript 2015 (ES6) standard.

The Map class provides a more convenient and intuitive way to create and work with dictionaries, and it has several additional features that are not available in the Object constructor.

To create a dictionary using the "Map" class, you can use the following syntax:

let myDict = new Map();

This creates a new empty dictionary that you can add key-value pairs to using the set method of the Map class. For example:

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

console.log(myDict);

Output

Map(2) { 'key1' => 'value1', 'key2' => 'value2' }

In this example, the set method is used to add two key-value pairs to the myDict dictionary. The set method takes two arguments: the key and the value to be added to the dictionary.

Once you have added some key-value pairs to a dictionary, you can access the values in the dictionary by their corresponding keys using the get method of the Map class. For example:

let valueOne = myDict.get("key1");
let valueTwo = myDict.get("key2");

console.log(valueOne, valueTwo);

Output

value1 value2

In this example, the get method is used to retrieve the values associated with the key1 and "key2" keys in the myDict dictionary. The values are assigned to the valueOne and valueTwo variables, respectively.

In addition to the set and get methods, the Map class also provides several other useful methods for working with dictionaries. For example, you can use the size method to get the number of key-value pairs in a dictionary, the has method to check if a dictionary contains a specific key, and the delete method to remove a key-value pair from a dictionary. For more information, you can refer to the reference section.

 

Summary

The Map class is another way to create and work with dictionaries in JavaScript. This class provides a more convenient and intuitive interface for dealing with dictionaries, and it offers several additional features that are not available in the Object constructor

 

References

Map - JavaScript | MDN (mozilla.org)

 

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