How to useNumerical Keys and Values in JavaScript in Javascript

In this approach initializes an empty object and adds key-value pairs using numerical keys, resulting in an object with properties assigned to respective values.

Example 1: This is the simplest as well as the native approach. In this approach, we will directly append the key-value pair(s) in that previously created empty object.

Javascript
let object = {};
let firstKey = 0;
let firstKeyValue = "w3wiki";
let secondKey = 1;
let secondKeyValue = "Hello JavaScript";
let thirdKey = 2;
let thirdKeyValue = "Hello React";

object[firstKey] = firstKeyValue;
object[secondKey] = secondKeyValue;
object[thirdKey] = thirdKeyValue;
console.log(object);

Output
{ '0': 'w3wiki', '1': 'Hello JavaScript', '2': 'Hello React' }

How to create an object from the given key-value pairs using JavaScript ?

In this article, we will learn to create an object from the given key-value pairs using JavaScript.Key-value pair: Unique identifier (key) associated with its corresponding value; fundamental for data organization and retrieval in programming. here we have some common approaches.

We will explore the process of creating objects from key-value pairs in JavaScript. Key-value pairs are fundamental in organizing and retrieving data efficiently within programs. We’ll discuss various common approaches to achieve this goal, empowering you with the knowledge to handle key-value data effectively in your JavaScript projects. Let’s see and discover these approaches together.

Following are some approaches to achieve the mentioned target.

Table of Content

  • Approach 1: Using Numerical Keys and Values in JavaScript
  • Approach 2: Using Object.assign()
  • Approach 3: Using for loop
  • Approach 4: Using a custom function
  • Approach 5: Using the Map object
  • Approach 6: Using Array.prototype.reduce()

We will explore all the above methods along with their basic implementation with the help of examples.

Similar Reads

Approach 1: Using Numerical Keys and Values in JavaScript

In this approach initializes an empty object and adds key-value pairs using numerical keys, resulting in an object with properties assigned to respective values....

Approach 2: Using Object.assign()

In this approach uses Object.assign() to add key-value pairs with numerical keys to an empty object, creating an object with properties assigned to corresponding values....

Approach 3: Using for loop

In this approach we are using for loop to create an object by iterating over two arrays containing keys and values, assigning properties to the object accordingly....

Approach 4: Using a custom function

The approach involves a custom function that takes arrays of keys and values as arguments. It iterates through the arrays, assigning properties to the object with corresponding keys and values....

Approach 5: Using the Map object

In this approach we are using the Map object and the Object.fromEntries() method to create an object from arrays of keys and values, effectively mapping numerical keys to their corresponding string values....

Approach 6: Using Array.prototype.reduce()

In this approach, we utilize the reduce method on arrays of keys and values to build an object. The reduce method iterates through the arrays, adding each key-value pair to the accumulator object....

Contact Us