Using JavaScript map() constructor, you can creates Map
objects. The map object is a data structure that stores elements as a key-value pair.
new Map()
new Map(iterable)
JavaScript map constructor
Simple example code creating a new Map. A new Map object returns after the initialization of the map constructor.
<!DOCTYPE html>
<html>
<body>
<script >
let myMap = new Map([
[1, 'one'],
[2, 'two'],
[3, 'three'],
]);
console.log(myMap)
</script>
</body>
</html>
Output:
Return key for JS Map constructor with value
You could convert it to an array of entries (using [...people.entries()]
) and search for it within that array.
let people = new Map();
people.set('1', 'jhon');
people.set('2', 'jasmein');
people.set('3', 'abdo');
let jhonKeys = [...people.entries()]
.filter(({ 1: v }) => v === 'jhon')
.map(([k]) => k);
console.log(jhonKeys); // if empty, no key found otherwise all found keys.
Output: [ “1” ]
Do comment if you have any doubts or suggestions on this Js map() tutorial.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version