Last updated: Mar 1, 2024
Reading timeยท3 min
Use destructuring assignment to get the first element of a Map
, e.g.
const [firstKey] = map.keys()
and const [firstValue] = map.values()
.
The keys()
and values()
methods return an iterator object that contains
the Map's keys and values.
const map = new Map(); map.set('site', 'bobbyhadz.com'); map.set('id', 1); const [firstKey] = map.keys(); console.log(firstKey); // ๐๏ธ site const [firstValue] = map.values(); console.log(firstValue); // ๐๏ธ bobbyhadz.com
We called the keys()
method on the Map
to get an iterator that contains the
keys of the elements in the Map
.
const map = new Map(); map.set('site', 'bobbyhadz.com'); map.set('id', 1); // ๐๏ธ [Map Iterator] { 'site', 'id' } console.log(map.keys()); const [firstKey] = map.keys(); console.log(firstKey); // ๐๏ธ site
We used destructuring assignment to get the first key and assigned it to a variable.
You can use the Map.values()
method to get an iterator of the Map's values.
const map = new Map(); map.set('site', 'bobbyhadz.com'); map.set('id', 1); // ๐๏ธ [Map Iterator] { 'bobbyhadz.com', 1 } console.log(map.values()); const [firstValue] = map.values(); console.log(firstValue); // ๐๏ธ bobbyhadz.com
The last step is to use the destructuring assignment to assign the first Map value to a variable.
An alternative approach is to convert the Map to an array.
This is a two-step process:
Map
object to an array.0
to get the first element of the Map
.const map = new Map(); map.set('site', 'bobbyhadz.com'); map.set('id', 1); const first = [...map][0]; console.log(first); // ๐๏ธ [ 'site', 'bobbyhadz.com' ]
We used the
spread syntax (...) to
convert the Map
to an array and then we accessed the array element at index
0
.
const map = new Map(); map.set('site', 'bobbyhadz.com'); map.set('id', 1); // ๐๏ธ [ [ 'site', 'bobbyhadz.com' ], [ 'id', 1 ] ] console.log([...map]); // ๐๏ธ [ 'site', 'bobbyhadz.com' ] console.log([...map][0]);
Accessing the first array element returns an array containing the key and value
of the first element in the Map
.
Map
with thousands of elements to an array would be slow and inefficient if you only have to access a single element.Alternatively, we can use some of the methods on the Map object.
Map.entries()
This is a three-step process:
Map.entries()
method to get an iterable of the Map's key-value
pairs.next()
method to get the first element of the iterable.const map = new Map(); map.set('site', 'bobbyhadz.com'); map.set('id', 1); const first = map.entries().next().value; // ๐๏ธ [ 'site', 'bobbyhadz.com' ] console.log(first); console.log(first[0]); // ๐๏ธ site console.log(first[1]); // ๐๏ธ bobbyhadz
We used the Map.entries()
method to get an iterable of the Map's key-value
pairs.
const map = new Map(); map.set('site', 'bobbyhadz.com'); map.set('id', 1); const iterable = map.entries(); // ๐๏ธ [Map Entries] { [ 'site', 'bobbyhadz.com' ], [ 'id', 1 ] } console.log(iterable); // ๐๏ธ { value: [ 'site', 'bobbyhadz.com' ], done: false } const firstIteration = iterable.next(); console.log(firstIteration); const first = firstIteration.value; console.log(first); // ๐๏ธ [ 'site', 'bobbyhadz.com' ]
We used the next()
method to get an object containing the value of the first
iteration.
Lastly, we accessed the value
property on the object to get an array of the
key and value of the first Map
element.
You can learn more about the related topics by checking out the following tutorials: