How to create dictionary and add key–value pairs dynamically?
Last Updated :
16 Sep, 2024
In JavaScript, dictionaries are implemented using objects. A dictionary, also known as a map, is a collection of key-value pairs. You can create a dictionary dynamically by defining an empty object and then adding key-value pairs using various methods. This article covers multiple approaches for dynamically creating and modifying dictionaries in JavaScript.
Below are the approaches for creating a dictionary and adding key-value pairs dynamically:
Using Object Literal Notation
Create a dictionary by defining an empty object and assigning key-value pairs using bracket or dot notation. This method provides a straightforward way to dynamically add and modify key-value pairs.
Example: To demonstrate creating a dictionary and add key-value pairs dynamically.
JavaScript
// Using object literal notation
let dictionary = {};
dictionary["key1"] = "value1";
dictionary["key2"] = "value2";
console.log(dictionary);
Output{ key1: 'value1', key2: 'value2' }
Using Object Constructor
Use the Object constructor to create a dictionary. Key-value pairs are added dynamically by assigning values to object properties. This approach is similar to object literal notation but uses a constructor function.
Example: To demonstrate creating a dictionary and add key-value pairs dynamically.
JavaScript
let dictionary = new Object();
dictionary["key1"] = "value1";
dictionary["key2"] = "value2";
console.log(dictionary);
Output{ key1: 'value1', key2: 'value2' }
Using ES6 Map
Create a dictionary using the Map object provided by ES6. Key-value pairs are added dynamically using the set()
method. Maps offer built-in methods for adding, retrieving, and deleting key-value pairs, providing flexibility and functionality.
Example: To demonstrate creating a dictionary and add key-value pairs dynamically.
JavaScript
let dictionary = new Map();
dictionary.set("key1", "value1");
dictionary.set("key2", "value2");
console.log(dictionary);
OutputMap(2) { 'key1' => 'value1', 'key2' => 'value2' }
Using Object.assign()
Combine an existing dictionary with new key-value pairs using Object.assign()
. This method copies the values of all enumerable own properties from one or more source objects to a target object. It is particularly useful for merging dictionaries dynamically.
Example: To demonstrate creating a dictionary and add key-value pairs dynamically.
JavaScript
// Using Object.assign()
let dictionary = {};
Object
.assign(dictionary, { "key1": "value1", "key2": "value2" });
console.log(dictionary);
Output{ key1: 'value1', key2: 'value2' }
Using Spread Operator
Utilize the spread operator to dynamically add key-value pairs to a dictionary. This operator allows for the creation of a new object with properties copied from another object, enabling the dynamic addition of key-value pairs without mutating the original dictionary.
Example: To demonstrate creating a dictionary and add key-value pairs dynamically.
JavaScript
// Using Spread Operator
let dictionary = {};
dictionary = { ...dictionary, "key1": "value1", "key2": "value2" };
console.log(dictionary);
Output{ key1: 'value1', key2: 'value2' }
Using Bracket Notation
Add key-value pairs to a dictionary dynamically using bracket notation. This approach involves accessing and modifying object properties using square brackets and allows for the dynamic creation of keys based on variables or expressions.
Example:To demonstrate creating a dictionary and add key-value pairs dynamically.
JavaScript
// Using bracket notation
let dictionary = {};
let key = "key1";
let value = "value1";
dictionary[key] = value;
console.log(dictionary);
Using Dot Notation
Dynamically add key-value pairs to a dictionary using dot notation. This method involves directly assigning values to object properties using dot notation and is commonly used when the keys are known in advance.
Example: To demonstrate creating a dictionary and add key-value pairs dynamically.
JavaScript
// Using dot notation
let dictionary = {};
dictionary.key1 = "value1";
dictionary.key2 = "value2";
console.log(dictionary);
Output{ key1: 'value1', key2: 'value2' }
Additional Tips:
- Choosing Between Objects and Maps: Use objects when you need a simple key-value pair storage. Opt for Map when you require keys of any type, ordered storage, or specific built-in methods.
- Avoid Key Collisions: When using objects, avoid keys that clash with built-in object properties (e.g., toString). Map does not have this issue.
- Performance Considerations: Maps are generally more performant when frequent additions and deletions are needed.
These methods provide flexible ways to dynamically create and manipulate dictionaries in JavaScript.
How to create dictionary and add key–value pairs dynamically?
Similar Reads
How to Add Same Key Value in Dictionary Python Dictionaries are powerful data structures that allow us to store key-value pairs. However, one common question that arises is how to handle the addition of values when the keys are the same. In this article, we will see different methods to add values for the same dictionary key using Python.Adding
2 min read
How to Add Duplicate Keys in Dictionary - Python In Python, dictionaries are used to store key-value pairs. However, dictionaries do not support duplicate keys. In this article, we will explore several techniques to store multiple values for a single dictionary key.Understanding Dictionary Key ConstraintsIn Python, dictionary keys must be unique.
3 min read
How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa
3 min read
How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa
3 min read
How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa
3 min read
How to Change the name of a key in dictionary? Dictionaries in Python are a versatile and powerful data structure, allowing you to store key-value pairs for efficient retrieval and manipulation. Sometimes, you might need to change the name of a key in a dictionary. While dictionaries do not directly support renaming keys, there are several ways
4 min read