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