A dictionary is a data structure that you can use to store data in your application. You can store data using key-value pairs. This allows you to search for and retrieve specific values.
Once you have the data in the dictionary, you can perform other actions such as iterating over each item. You can also check if items exist or delete items you no longer need.
How to create a dictionary object
A dictionary is one of many important data structures that can be used to store data. You can create dictionaries in C# and many other programming languages. You can also create an equivalent hashmap data structure in Java.
There is no “dictionary” keyword that can be used to create dictionary objects in JavaScript. However, you can create a dictionary using generic objects. Here is an example of how to create an empty dictionary using the “Object” keyword.
let dictionary = new Object();
You can also create an empty dictionary using the shorthand syntax:
let emptyDictionary = {};
If you want to initialize the dictionary with values, you can add each value in the format “key:value”.
Using the example below, you can create a string key called “Pidgey” and associate it with a value. The value is an object with properties for the pet’s age, color, and gender.
let petDictionary = {
"Pidgey": { Age: 0.5, Color: "Gray", Gender: "Male" },
"Mocha": { Age: 0.5, Color: "Brown", Gender: "Female" },
};
Keys are not limited to string data types. You can use other data types, such as numbers and booleans.
let wcDictionary = {
1: { Team: "Argentina" },
2: { Team: "France" },
};let dictBool = {
true: { Message: "Confirmed" },
false: { Message: "Denied" },
};
How to add values to a Dictionary object
You can add new items to the dictionary using the following format:
dictionary[new_key] = new_value
new_key can be any valid key value. This is the key that you will use later to access specific items in the dictionary. new_value can be any object or value to associate with the key.
Here is an example of how to add a new item to the dictionary with some example values.
petDictionary["Apples"] = { Age: 2, Color: "Green", Gender: "Male" };
As with initialization, other data types can also be used to represent keys.
wcDictionary[3] = { Team: "Morocco" };
How to access the value based on the key
You can access the values from the dictionary using the key value.
let dictionaryValue = petDictionary["Mocha"];
console.log(dictionaryValue);
The returned value contains the entire object or value stored at that key.
How to iterate over each item in a dictionary
You can iterate over each item in the dictionary using the Object.keys() method. The Object.Keys() method returns an array containing all keys used in the dictionary.
console.log(Object.keys(petDictionary));
The console will display an array containing all the keys of the dictionary.
With a list of keys, you can loop through each item in the dictionary and get the value for each key.
for (const key of Object.keys(petDictionary)) {
console.log(key + ": ");
console.log(petDictionary[key]);
};
You should see the following result in the console:
How to check if an item exists in a dictionary
You can use the ‘in’ keyword to check if a key exists in the dictionary.
let inDictionary = 'Mocha' in petDictionary;
let notInDictionary = 'a' in petDictionary;
You can also use the hasOwnProperty() method to check if an item exists.
let exists = petDictionary.hasOwnProperty('Mocha');
let doesNotExist = petDictionary.hasOwnProperty('a');
How to remove value from dictionary
An item can be set to null to indicate that it has no value.
petDictionary['Apples'] = null;
However, the item still exists in the dictionary. If you want to permanently delete an item, you can do so using the ‘delete’ keyword.
delete petDictionary['Apples'];
Storing data in a dictionary in JavaScript
JavaScript doesn’t have first-class support for dictionaries, but you can use plain objects to store key-value pairs.
A dictionary is a very powerful data structure that can be used to store and access data using keys. Dictionaries aren’t the only place you can store data, so you can explore other data structures that are better suited to your use case.