Map to Array in JavaScript
Last Updated :
29 Jun, 2023
In this article, we will convert a Map object to an Array in JavaScript. A Map is a collection of key-value pairs linked with each other. The following are the approaches to Map to Array conversion:
Methods to convert Map to Array
Using the Spread operator(...) and map() method, we can take each entry and convert it into an array of key-value pairs.
Syntax:
let Array = [...value];
// Arrow function
Array.map((e) => e);
Array.map((e, i) => {i,e});
Example: This example illustrates the implementation of the Spread Operator (...) Method and Array map() Method.
JavaScript
const mapObj = new Map();
mapObj.set(1, 'abc');
mapObj.set(2, 'xyz');
mapObj.set(3, 'pqr');
// Return the map
console.log(mapObj);
let arr = [...mapObj].map((e) => e)
// Return the array
console.log(arr);
OutputMap(3) { 1 => 'abc', 2 => 'xyz', 3 => 'pqr' }
[ [ 1, 'abc' ], [ 2, 'xyz' ], [ 3, 'pqr' ] ]
Approach 2: Using Array.from() Method
The Array.from() Method returns a new instance of an array from the object provided in the arguments.
Syntax:
Array.from(object, mapFunction, thisValue);
Example: In this example, we will pass a map object and a function into Array.from() Method to convert it into an array.
JavaScript
const mapObj = new Map();
mapObj.set(1, 'abc');
mapObj.set(2, 'xyz');
mapObj.set(3, 'pqr');
// Return the map
console.log(mapObj);
// Return the array
let arr = Array.from(mapObj);
console.log(arr);
OutputMap(3) { 1 => 'abc', 2 => 'xyz', 3 => 'pqr' }
[ [ 1, 'abc' ], [ 2, 'xyz' ], [ 3, 'pqr' ] ]
JavaScript map.forEach() method is used to iterate the map object and execute the callback function provided in the arguments.
Syntax:
mymap.forEach();
Example: This example illustrates the implementation of the map.forEach() Method.
JavaScript
const mapObj = new Map();
mapObj.set(1, 'abc');
mapObj.set(2, 'xyz');
mapObj.set(3, 'pqr');
// Return the map
console.log(mapObj);
const arr = [];
mapObj.forEach(
(value, key) => arr.push(
{ key, value }));
// Return the array
console.log(arr);
OutputMap(3) { 1 => 'abc', 2 => 'xyz', 3 => 'pqr' }
[
{ key: 1, value: 'abc' },
{ key: 2, value: 'xyz' },
{ key: 3, value: 'pqr' }
]
There are many inbuilt JavaScript methods that can be used to transform data from one data type to another. Above mentioned are a few of those that can be easily implemented to convert the Map object to an array.
Similar Reads
JavaScript Array map() Method The map() method is an ES5 feature that creates a new array by applying a function to each element of the original array. It skips empty elements and does not modify the original array.JavaScriptconst a = [1, 2, 3, 4]; // Use map to create a new array with elements doubled const b = a.map(x => x
4 min read
Types of Arrays in JavaScript A JavaScript array is a collection of multiple values at different memory blocks but with the same name. The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]). A JavaScript array can be classi
3 min read
Set to Array in JS or JavaScript This article will show you how to convert a Set to an Array in JavaScript. A set can be converted to an array in JavaScript in the following ways:1. Using Spread OperatorThe JavaScript Spread Operator can be used to destructure the elements of the array and then assign them to a new variable in the
2 min read
JavaScript Array from() Method The Array.from() method is used to create a new array from any iterables like array, objects, and strings.JavaScriptconst a = Array.from("GeeksforGeeks"); console.log(a);Output[ 'G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's' ] SyntaxArray.from(object, mapFunction, thisValue);Paramet
2 min read
JavaScript Array from() Method The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object. Syntax : Array.from(object, mapFunction, thisValue)Parameters:object: This Parameter is required to specify the object to convert to an array.mapFunction: This Parameter specifies
3 min read
How to Sort a Map in JavaScript? Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach
3 min read
Set vs Map in JavaScript In JavaScript, Set and Map are two types of objects that are used for storing data in an ordered manner. Both these data structures are used to store distinct types of data inside the same object. In Maps, the data is stored as a key-value pair whereas in Set data is a single collection of values th
2 min read
Applications of Map in JavaScript Javascript Map is a collection of elements where each element is stored as a Key, value pair. Map objects can hold both objects and primitive values as either key or value. When we iterate over the map object it returns the key, and value pair in the same order as inserted. Syntax: new Map([it]); Pa
5 min read
JavaScript Array flatMap() Method The flatMap() method transforms each element of an array using a mapping function and flattens the result into a new array. It applies the function to every element, avoiding empty ones, and preserves the original array. Syntax:let A = array.flatMap(function callback(current_value, index, Array)) {/
3 min read
JavaScript Array with() Method The with() method in JavaScript Array returns a new array with the element at the given index replaced with the given value. Syntax:input_array.with(index, value)Parameters: Index: RangeError is thrown if the provided index does not exist in the array. value: It is the value that is assigned to the
1 min read