Last updated: Mar 2, 2024
Reading timeยท4 min
To convert a Map
to an array of objects:
Map
and a function to the Array.from()
method.Map
in the function and return an object containing the
current key-value pair.Array.from()
method will convert the Map to an array of objects.const map = new Map(); map.set('name', 'Bob'); map.set('country', 'Chile'); const arr = Array.from(map, ([key, value]) => { return {[key]: value}; }); // ๐๏ธ [{name: 'Bob'}, {country: 'Chile'}] console.log(arr);
You can format the array of objects differently depending on your use case.
const map = new Map(); map.set('name', 'Bob'); map.set('country', 'Chile'); const arr = Array.from(map, ([key, value]) => { return {key, value}; }); // ๐๏ธ [ { key: 'name', value: 'Bob' }, // { key: 'country', value: 'Chile' } ] console.log(arr);
We passed the following arguments to the Array.from() method:
map
function that gets called with each element in the array.If we call the Array.from()
method without the map
function, we get a
two-dimensional array.
const map = new Map(); map.set('name', 'Bob'); map.set('country', 'Chile'); // ๐๏ธ [['name', 'Bob'], ['country', 'Chile']] const arr = Array.from(map); console.log(arr);
The function we passed to the Array.from()
method gets called with each
element in the array.
We used the square bracket []
syntax to destructure the elements of the array
in the arrow function.
const [a, b] = ['hello', 'world']; console.log(a); // ๐๏ธ hello console.log(b); // ๐๏ธ world
Array destructuring enables us to unpack multiple array values into variables in a single statement.
Whatever we return from this function gets added to the return value of
Array.from()
.
map (Array.map)
function. It allows us to iterate over an array and create a new array based on the returned values.We used the dynamic property key syntax []
to set the key of the object to the
actual key of the Map
.
const arr = Array.from(map, ([key, value]) => { // ๐๏ธ return {[key]: value}; });
A simple way to think about the dynamic key assignment is that we are evaluating
the variable key
and setting the object's key to the result.
const a = 'name'; const obj = { [a]: 'Bob', }; console.log(obj); // ๐๏ธ {name: 'Bob'}
Here's an example that achieves the same result by using the Array.map()
method separately.
This is a three-step process:
Map
to an array.Array.map()
method to iterate over the array.const map = new Map(); map.set('name', 'Bob'); map.set('country', 'Chile'); // ๐๏ธ [['name', 'Bob'], ['country', 'Chile']] const arr = Array.from(map); console.log(arr); // ๐๏ธ [{name: 'Bob', country: 'Chile'}] const arrOfObj = arr.map(([key, value]) => { return {[key]: value}; }); console.log(arrOfObj);
You can format the array of objects differently depending on your use case.
const map = new Map(); map.set('name', 'Bob'); map.set('country', 'Chile'); // ๐๏ธ [['name', 'Bob'], ['country', 'Chile']] const arr = Array.from(map); console.log(arr); // ๐๏ธ [ { key: 'Bob' }, { key: 'Chile' } ] const arrOfObj = arr.map(([key, value]) => { return {key: value}; }); console.log(arrOfObj);
Instead of passing the
Array.map()
function as a parameter to the Array.from()
method, we used it separately.
Everything works in the same way as in the previous code sample, however, the code is a little less performant.
You can also use the Map.forEach()
method to convert a Map to an array of
objects.
This is a three-step process:
Map.forEach()
method to iterate over the Map.const map = new Map(); map.set('name', 'Bob'); map.set('country', 'Chile'); const arr = []; map.forEach((value, key) => { arr.push({key, value}); }); // ๐๏ธ [ { key: 'name', value: 'Bob' }, { key: 'country', value: 'Chile' } ] console.log(arr);
The function we passed to the Map.forEach()
method gets called for each key-value pair in the Map
object.
On each iteration, we wrap the key and value into an object and push the object into an array.
You can also use a for..of
loop to achieve the same result.
const map = new Map(); map.set('name', 'Bob'); map.set('country', 'Chile'); const arr = []; for (const [key, value] of map) { arr.push({key, value}); } // ๐๏ธ [ { key: 'name', value: 'Bob' }, { key: 'country', value: 'Chile' } ] console.log(arr);
The for...of statement is
used to loop over iterable objects like arrays, strings, Map
, Set
and
NodeList
objects and generators
.
On each iteration, we
add the key-value pair of the Map
to an object
and push the object into the array.
Which approach you pick is a matter of personal preference. I'd use the
Array.from()
method with a map
function because I find it quite direct and
intuitive.
You can learn more about the related topics by checking out the following tutorials: