How to use Array.from() and find() method In Javascript

In this approach, we are using Array.from() to convert the Map entries into an array of key-value pairs. Then, we apply the find() method to search for the entry where the key matches the specified key.

Syntax

Array.from(object, mapFunction, thisValue)
array.find(function(currentValue, index, arr), thisValue)
JavaScript
const map = new Map();
map.set('name', 'w3wiki');
map.set('CEO', 'Sandeep Jain');
const key = 'name';
const value = Array.from(map).find(([k, v]) => k === key)?.[1];
console.log(value);

Output
w3wiki


How to get the Value by a Key in JavaScript Map?

JavaScript Map is a powerful data structure that provides a convenient way to store key-value pairs and retrieve values based on keys. This can be especially useful when we need to associate specific data with unique identifiers or keys.

Similar Reads

Different Approaches to Get the Value by a Key in JavaScript Map

Table of Content Using the get() methodUsing forEach method...

1. Using the get() method

The get method of the Map object is used to retrieve the value associated with a specified key....

2. Using forEach method

In this approach, iterate we will use forEach method to iterates over each key-value pair in the Map. It takes a callback function as an argument, which is called with each key and value in the Map. In this case, the callback function checks if the key matches the desired key and assigns the corresponding value to the value variable....

3. Using Array.from() and find() method

In this approach, we are using Array.from() to convert the Map entries into an array of key-value pairs. Then, we apply the find() method to search for the entry where the key matches the specified key....