Convert Map values to array in JavaScript
Question:
How to convert Map values to array in JavaScript? Answer:
let myArray = Array.from(myMap.values());
Description:
You can easily convert all the values of a Map object to a simple array in JavaScript using the from()
method of the built-in Array
object.
Let's say you have the following Map:
let myMap = new Map([
['2023-08-01', 2],
['2023-08-02', 1],
['2023-08-03', 3]
]);
To create an array that contains only the values (2,1 and 3) use the following code:
const myArray = Array.from(myMap.values());
And the result is:
// [2, 1, 3]
Reference:
JavaScript Array.from reference
Share "How to convert Map values to array in JavaScript?"
Related snippets:
- Check if an array contains a value
- Append an array to another in JavaScript
- Get the first element of an array in JavaScript
- Convert Map values to array in JavaScript
- Convert array to comma separated string using JavaScript
- Get the minimum value in an array using JavaScript
- Get the maximum value in an array using JavaScript
- Remove duplicates from array in JavaScript
- Split a string into words
- Use foreach in JavaScript
Tags:
map, array, values, convert, javascript Technical term:
Convert Map values to array in JavaScript