How to convert a map to array of objects in JavaScript?
Last Updated :
25 Jul, 2024
A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods for converting a map into an array of objects in JavaScript.
Example:
Input Map: Map(2)
{
'Company' => 'GeeksforGeeks',
'Cricketer' => 'Virat Kohli'
}
Output Array:
[
{ type: 'Company', name: 'GeeksforGeeks' },
{ type: 'Cricketer', name: 'Virat Kohli' }
]
The below methods can be used to convert a map into an array of objects in JavaScript:
Using the Array.from() method
The Array.from() method of JavaScript can be used to convert a map into an array of objects by passing the map and a callback function with the names of object keys as parameters to it.
Syntax:
Array.from(mapName, ([Objectkeys...])=>{});
Example: The below code is a practical implementation of the above-discussed approach.
JavaScript
const myMap = new Map();
myMap.set("Company", "GeeksforGeeks");
myMap.set("Cricketer", "Virat Kohli");
console.log("Map Elements: ", myMap);
const objectsArray = Array.from
(myMap, ([type, name]) => ({ type, name }));
console.log("Array of Objects: ", objectsArray);
OutputMap Elements: Map(2) { 'Company' => 'GeeksforGeeks', 'Cricketer' => 'Virat Kohli' }
Array of Objects: [
{ type: 'Company', name: 'GeeksforGeeks' },
{ type: 'Cricketer', name: 'Virat Kohli' }
]
Using the spread operator syntax
The spread operator syntax can be used to destructure the map and then iterate through each element using the map() method with a callback function to push them into an array of objects.
Syntax:
[...mapName].map(([ObjectKeys...])=>{});
Example: The below code converts a map into an array of objects using the spread operator syntax.
JavaScript
const myMap = new Map();
myMap.set("Company", "GeeksforGeeks");
myMap.set("Cricketer", "Virat Kohli");
console.log("Map Elements: ", myMap);
const objectsArray =
[...myMap].map(([type, name]) => ({ type, name }));
console.log("Array of Objects: ", objectsArray);
OutputMap Elements: Map(2) { 'Company' => 'GeeksforGeeks', 'Cricketer' => 'Virat Kohli' }
Array of Objects: [
{ type: 'Company', name: 'GeeksforGeeks' },
{ type: 'Cricketer', name: 'Virat Kohli' }
]
Using the Array.from() with Array.map() method
In this approach, the Array.from() method will destructure the elements of the map and then the map() method will iterate through them with a callback function to convert them into a array of objects.
Syntax:
Array.from(mapName).map(([objectKeys...])=>{});
Example: This code example implements the Array.from() and Array.map() methods to convert a map into an array of objects.
JavaScript
const myMap = new Map();
myMap.set("Company", "GeeksforGeeks");
myMap.set("Cricketer", "Virat Kohli");
console.log("Map Elements: ", myMap);
const objectsArray =
Array.from(myMap).map(([type, name]) => ({ type, name }));
console.log("Array of Objects: ", objectsArray);
OutputMap Elements: Map(2) { 'Company' => 'GeeksforGeeks', 'Cricketer' => 'Virat Kohli' }
Array of Objects: [
{ type: 'Company', name: 'GeeksforGeeks' },
{ type: 'Cricketer', name: 'Virat Kohli' }
]
Using the forEach() method
The forEach() method can be directly used to iterate the elements of the map and then push them into an empty array one by one with the help of the push() method.
Syntax:
mapName.forEach((objectKeys...)=>{arrayName.push()});
Example: The below code explains the use of the forEach() method to convert a map into an array of objects.
JavaScript
const myMap = new Map();
myMap.set("Company", "GeeksforGeeks");
myMap.set("Cricketer", "Virat Kohli");
console.log("Map Elements: ", myMap);
const objectsArray = [];
myMap.forEach
((name, type) => objectsArray.push({ name, type }));
console.log("Array of Objects: ", objectsArray);
OutputMap Elements: Map(2) { 'Company' => 'GeeksforGeeks', 'Cricketer' => 'Virat Kohli' }
Array of Objects: [
{ name: 'GeeksforGeeks', type: 'Company' },
{ name: 'Virat Kohli', type: 'Cricketer' }
]
Using the for of loop
The for of loop can also be used to iterate through the map elements and push them into an array as done in the last approach.
Syntax:
for(const [objectKeys...] of mapName){ arrayName.push() };
Example: The below code will illustrate the use of the for of loop to convert a map into an array in JavaScript.
JavaScript
const myMap = new Map();
myMap.set("Company", "GeeksforGeeks");
myMap.set("Cricketer", "Virat Kohli");
console.log("Map Elements: ", myMap);
const objectsArray = [];
for (const [name, type] of myMap) {
objectsArray.push({ name, type });
}
console.log("Array of Objects: ", objectsArray);
OutputMap Elements: Map(2) { 'Company' => 'GeeksforGeeks', 'Cricketer' => 'Virat Kohli' }
Array of Objects: [
{ name: 'Company', type: 'GeeksforGeeks' },
{ name: 'Cricketer', type: 'Virat Kohli' }
]
Using the Object.fromEntries() Method
The Object.fromEntries() method transforms a list of key-value pairs into an object. We can use this method in combination with Object.entries() to create an array of objects from a map.
Example: The below example uses the Object.fromEntries() method to convert a map into an array of objects.
JavaScript
const myMap = new Map();
myMap.set("Company", "GeeksforGeeks");
myMap.set("Cricketer", "Virat Kohli");
console.log("Map Elements: ", myMap);
const mapObject = Object.fromEntries(myMap);
const objectsArray = Object.entries(mapObject).map(([type, name]) => ({ type, name }));
console.log("Array of Objects: ", objectsArray);
OutputMap Elements: Map(2) { 'Company' => 'GeeksforGeeks', 'Cricketer' => 'Virat Kohli' }
Array of Objects: [
{ type: 'Company', name: 'GeeksforGeeks' },
{ type: 'Cricketer', name: 'Virat Kohli' }
]
Using the reduce() method
The reduce() method can be used to iterate through the map entries and construct an array of objects by accumulating the key-value pairs in an array.
Example: The following code demonstrates the use of the reduce() method to convert a map into an array of objects:
JavaScript
const myMap = new Map();
myMap.set("Company", "GeeksforGeeks");
myMap.set("Cricketer", "Virat Kohli");
console.log("Map Elements: ", myMap);
const objectsArray = Array.from(myMap).reduce((acc, [type, name]) => {
acc.push({ type, name });
return acc;
}, []);
console.log("Array of Objects: ", objectsArray);
OutputMap Elements: Map(2) { 'Company' => 'GeeksforGeeks', 'Cricketer' => 'Virat Kohli' }
Array of Objects: [
{ type: 'Company', name: 'GeeksforGeeks' },
{ type: 'Cricketer', name: 'Virat Kohli' }
]
Using the Array.prototype.flatMap() Method
The Array.prototype.flatMap() method is used to first map each element using a mapping function and then flatten the result into a new array. It can be employed to convert a map into an array of objects by mapping over the map entries and transforming each entry into the desired object format.
Example: The below code demonstrates how to use the flatMap() method to convert a map into an array of objects:
JavaScript
const myMap = new Map();
myMap.set("Company", "GeeksforGeeks");
myMap.set("Cricketer", "Virat Kohli");
console.log("Map Elements: ", myMap);
const objectsArray = [...myMap].flatMap(([type, name]) => [{ type, name }]);
console.log("Array of Objects: ", objectsArray);
OutputMap Elements: Map(2) { 'Company' => 'GeeksforGeeks', 'Cricketer' => 'Virat Kohli' }
Array of Objects: [
{ type: 'Company', name: 'GeeksforGeeks' },
{ type: 'Cricketer', name: 'Virat Kohli' }
]
Using a Generator Function
You can use a generator function to yield each key-value pair from the map as an object and then convert the generator's result to an array.
Example:
JavaScript
const myMap = new Map();
myMap.set("Company", "GeeksforGeeks");
myMap.set("Student", "Nikunj Sonigara");
console.log("Map Elements: ", myMap);
function* mapToObjects(map) {
for (const [type, name] of map) {
yield { type, name };
}
}
const objectsArray = Array.from(mapToObjects(myMap));
console.log("Array of Objects: ", objectsArray);
OutputMap Elements: Map(2) { 'Company' => 'GeeksforGeeks', 'Student' => 'Nikunj Sonigara' }
Array of Objects: [
{ type: 'Company', name: 'GeeksforGeeks' },
{ type: 'Student', name: 'Nikunj Sonigara' }...
Similar Reads
How to Convert an Array of Objects to Map in JavaScript?
Here are the different methods to convert an array of objects into a Map in JavaScript1. Using new Map() ConstructorThe Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.JavaScriptcon
3 min read
How to Convert Object to Array in JavaScript?
In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such
4 min read
How to Convert String of Objects to Array in JavaScript ?
This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i
3 min read
How to Convert String to Array of Objects JavaScript ?
Given a string, the task is to convert the given string to an array of objects using JavaScript. It is a common task, especially when working with JSON data received from a server or API. Below are the methods that allow us to convert string to an array of objects:Table of ContentUsing JSON.parse()
4 min read
How to Convert an Object into Array of Objects in JavaScript?
Here are the different methods to convert an object into an array of objects in JavaScript1. Using Object.values() methodObject.values() method extracts the property values of an object and returns them as an array, converting the original object into an array of objects.JavaScriptconst a = { java:
3 min read
How to convert a Set to Map in JavaScript?
A Set is a collection of unique values. It stores only the data in the form of values while a Map can store the data in the form of key-value pairs. These are the different ways of converting a Set to a Map in JavaScript: Table of Content Using the Array.from() methodUsing the Spread Operator with m
3 min read
How To Convert Map Keys to an Array in JavaScript?
Here are the various methods to convert Map keys to an array in JavaScript1. Using array.from() MethodThe Array.from() method in JavaScript converts Map keys to an array by using 'Array.from(map.keys())'. JavaScriptlet map = new Map().set('GFG', 1).set('Geeks', 2); let a = Array.from(map.keys()); co
2 min read
How to Convert Array into Array of Objects using map() & reduce() in JavaScript?
An array of objects can contain multiple objects with the same properties i.e. key-value pairs. But a map does not contain duplicate values which means if you convert an array of objects with duplicate objects into a map, it will contain only the unique key-value pairs. Below are the approaches to c
2 min read
How to Access Array of Objects in JavaScript ?
Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How
4 min read
How to Convert JSON to ArrayBuffer in JavaScript?
An ArrayBuffer is a complex data type, and structures which has a fixed length and takes binary content as the whole. Any variable that contains pure binary data will be defined in JavaScript as Simple Data, however on some occasions it is sometimes necessary to convert JSON data to an ArrayBuffer,
4 min read