- Instant help with your JavaScript coding problems

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]
Share "How to convert Map values to array in JavaScript?"