Open In App

How to create dictionary and add key–value pairs dynamically?

Last Updated : 16 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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);

Output
Map(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);

Output
{ key1: 'value1' }

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy