How to Get the First Element of a Map in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
3 min

banner

# Get the First Element of a Map in JavaScript

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.

index.js
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

get first element of map

We called the keys() method on the Map to get an iterator that contains the keys of the elements in the Map.

index.js
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.

index.js
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.

# Get the First Element of a Map using spread syntax (...)

This is a two-step process:

  1. Use the spread syntax (...) to convert the Map object to an array.
  2. Access the array at index 0 to get the first element of the Map.
index.js
const map = new Map(); map.set('site', 'bobbyhadz.com'); map.set('id', 1); const first = [...map][0]; console.log(first); // ๐Ÿ‘‰๏ธ [ 'site', 'bobbyhadz.com' ]

get first element of map using spread syntax

We used the spread syntax (...) to convert the Map to an array and then we accessed the array element at index 0.

index.js
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.

Note that converting a 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.

# Get the First Element of a Map using Map.entries()

This is a three-step process:

  1. Use the Map.entries() method to get an iterable of the Map's key-value pairs.
  2. Use the next() method to get the first element of the iterable.
  3. Assign the value to a variable.
index.js
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

get first element of map using map entries

We used the Map.entries() method to get an iterable of the Map's key-value pairs.

index.js
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev