Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Mapping Values to Keys in JavaScript



In the given problem statement we have to write a method for mapping values to keys with the help of Javascript. So for doing this task we will use an object literal in Javascript.

Understanding the problem statement

So we have to find out the value for the given key and for doing this task we can use an object literal. An object is a data structure which can store the data in key-value pairs. So the keys are the identifier of the values. With the help of keys we can access the values. In Javascript there are several methods to access the key-value pair. The most popular and used method is using dot or bracket notation. With the help of dot and bracket notation we can access the value of a particular key directly without creating any functions.

For example we have an object like const obj = {1: 'value1', 2: 'value2', 3: 'value3'}, in this if we have to access the value of key 3 then we can access using dot or bracket notation like 'obj.3' or obj[3].

Logic for the above problem

In the solution code we will create an object with predefined key and value. And access the values with the help of do or bracket notation. This technique to access a value of an object is a constant amount of operation. This means this technique will take the same amount of time besides the size of the object.

We will also provide a solution for map values to keys dynamically with the usage of a loop. This process involves iterating over the given array of values and adding them to the object with the help of bracket notation.

Algorithm - Using dot and bracket notation

Step 1 ? Use an object with key and value.

Step 2 ? Use a dot and bracket notation to access the value of the provided key.

Step 3 ? Show the result as the value of the keys.

Code for the algorithm - Using dot and bracket notation

const map = {
   key1: 'value1',
   key2: 'value2',
   key3: 'value3'
};
console.log(map.key1);
console.log(map['key2']);

Algorithm - Using map

Step 1 ? Define an array and give it name as values and put some data with values.

Step 2 ? Use a map object and initialize it as empty.

Step 3 ? With the help of a for loop we will iterate through the elements of the array and access the values as per the index keys using map object.

Step 4 ? Show the output for accessing the values of the particular keys.

Code for the algorithm - Using map

const values = ['value1', 'value2', 'value3'];
const map = {};

for (let i = 0; i < values.length; i++) {
   map[`key${i + 1}`] = values[i];
}
console.log(map.key1);
console.log(map['key2']);

Complexity

The time taken by the first algorithm for accessing a value in an object with the help of dot or bracket notation is O(1). This means this is a constant time procedure besides the size of the object. And for the second method the time complexity is O(n) in this n is the length of the values array.

Conclusion

In these implementations we have accessed the values for provided keys using object literal and with the help of mapping the values to keys dynamically. So the first method is very straightforward but in the second method we have used a loop to add the key-value pairs to the object called map.

Updated on: 2023-05-18T14:56:16+05:30

676 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements