JavaScript How to Convert Map to Array Easily
You can convert a Map to an array in JavaScript using
Array.from(map) or the spread operator like [...map], which creates an array of key-value pairs.Examples
Inputnew Map([[1, 'a'], [2, 'b']])
Output[[1, 'a'], [2, 'b']]
Inputnew Map([['name', 'John'], ['age', 30]])
Output[['name', 'John'], ['age', 30]]
Inputnew Map()
Output[]
How to Think About It
To convert a Map to an array, think of the Map as a collection of pairs (key and value). You want to create a list (array) where each item is one of these pairs. JavaScript provides easy ways to turn this collection into an array by extracting all pairs at once.
Algorithm
1
Get the Map object as input.2
Use a method to extract all key-value pairs from the Map.3
Create a new array containing these pairs.4
Return the new array.Code
javascript
const map = new Map([[1, 'a'], [2, 'b']]); const arrayFromMap = Array.from(map); console.log(arrayFromMap); const spreadArray = [...map]; console.log(spreadArray);
Output
[[1, 'a'], [2, 'b']]
[[1, 'a'], [2, 'b']]
Dry Run
Let's trace converting Map([[1, 'a'], [2, 'b']]) to an array.
1
Start with Map
Map contains pairs: (1, 'a'), (2, 'b')
2
Use Array.from(map)
Extract pairs into array: [[1, 'a'], [2, 'b']]
3
Result
Array with pairs: [[1, 'a'], [2, 'b']]
| Step | Map Content | Array Result |
|---|---|---|
| 1 | Map with (1, 'a'), (2, 'b') | N/A |
| 2 | N/A | [[1, 'a'], [2, 'b']] |
Why This Works
Step 1: Map stores pairs
A Map holds data as key-value pairs, not as a list.
Step 2: Array.from extracts pairs
Array.from(map) takes all pairs and puts them into an array.
Step 3: Spread operator does the same
Using [...map] spreads all pairs into a new array.
Alternative Approaches
Using forEach to build array
javascript
const map = new Map([[1, 'a'], [2, 'b']]); const arr = []; map.forEach((value, key) => { arr.push([key, value]); }); console.log(arr);
More manual but useful if you want to process pairs while converting.
Using map.keys() or map.values()
javascript
const map = new Map([[1, 'a'], [2, 'b']]); const keysArray = [...map.keys()]; const valuesArray = [...map.values()]; console.log(keysArray); console.log(valuesArray);
Extracts only keys or only values as arrays, not pairs.
Complexity: O(n) time, O(n) space
Time Complexity
Converting a Map to an array requires visiting each pair once, so it takes linear time proportional to the number of pairs.
Space Complexity
A new array is created to hold all pairs, so space used grows linearly with the Map size.
Which Approach is Fastest?
Using Array.from() or the spread operator [...map] are equally efficient and concise; manual loops are more verbose but allow custom processing.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Array.from(map) | O(n) | O(n) | Simple and direct conversion |
| [...map] | O(n) | O(n) | Concise and readable syntax |
| forEach loop | O(n) | O(n) | Custom processing during conversion |
| map.keys()/map.values() | O(n) | O(n) | Extracting only keys or values |
Use
[...map] for a quick and readable way to convert a Map to an array.Trying to convert a Map directly with
map.toArray() which does not exist.