Open In App

JavaScript Program to get Key by Value in a Map

In this article, we will see how to get a key by a value in a Map in JavaScript.Getting a key by a specific value in a JavaScript Map refers to finding the corresponding key associated with a given value stored within the Map collection.

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



Approach 1: Using map.entries() and for…of loop

In this approach,

Syntax:



mapObj.entries()   //map.entries()
//for..of loop
for ( variable of iterableObjectName) {
...
};

Example: In this example we are using the above-explained apporach.




function keyValue(map, searchKey) {
    for (const [key, value] of map.entries()) {
        if (value === searchKey)
            return key;
    }
    return undefined;
}
  
let students = new Map();
students.set('1', 'Diya');
students.set('2', 'Arav');
students.set('3', 'Priyanka');
students.set('4', 'Raj');
  
console.log(
    "Key associated with the value is: " + keyValue(students, 'Diya'));

Output
Key associated with the value is: 1

Approach 2: Using Map.forEach()

In this approach,

Syntax:

myMap.forEach(callback, value, key, thisArg)

Example: In this example we are using the above-explained approach.




function KeyByValue(map, KeyValue) {
    let result;
    map.forEach((value, key) => {
        result = value === KeyValue ? key : result;
    });
    return result;
}
  
const students = new Map();
students.set('1', 'Diya');
students.set('2', 'Arav');
students.set('3', 'Priyanka');
students.set('4', 'Raj');
  
console.log(
    "Key associated with the value is: " + KeyByValue(students, 'Raj'));

Output
Key associated with the value is: 4

Approach 3: Using spread syntax, Array.filter(), and Array.map()

In this approach:

Syntax:

let variablename1 = [...value];   //spread operator
array.filter(callback(element, index, arr), thisValue) //array.filter()
map((element, index, array) => { /* … */ }) //array.map()

Example: In this example we are using the above-explained apporach.




function getKeyByValue(myMap, searchKeyByValue) {
    let myKey = [...myMap.entries()].filter(({ 1: v }) =>
        v === searchKeyByValue).map(([k]) => k);
    return myKey;
  
}
  
let students = new Map();
students.set('1', 'Diya');
students.set('2', 'Arav');
students.set('3', 'Priyanka');
students.set('4', 'Raj');
  
let key = getKeyByValue(students, 'Priyanka');
if (key != "")
    console.log("Key associated with the value is: " + key);
else
    console.log("Value does not exist in the map");

Output
Key associated with the value is: 3

Approach 4: Using Array.from() and Array.find() method

In this approach, we are using Array.from()

Syntax:

Array.from(object, mapFunction, thisValue)  //array.form()
array.find(function(currentValue, index, arr),thisValue); //array.find()

Example: In this example we are using the above-explained apporach.




let students = new Map();
students.set('1', 'Diya');
students.set('2', 'Arav');
students.set('3', 'Priyanka');
students.set('4', 'Raj');
  
let arr1 = Array.from(students.entries());
const foundEntry = 
    arr1.find(([key, value]) => value === 'Diya');
  
let result = foundEntry ? foundEntry[0] : undefined;
console.log(
    "Key associated with the value is: " + result);

Output
Key associated with the value is: 1

Article Tags :