How to convert Map Values to an Array in JavaScript-Code Examples

Victor Talmacinschi

How to convert Map Values to an Array in JavaScript

JavaScript by Victor Talmacinschi June 21, 2022

const map = new Map();

map.set('🍎', 'Apple');
map.set('🍊', 'Orange');
map.set('🍋', 'Lemon');

// To convert the values of a Map to an Array, you can use the `Array.from()` method:
const mapValues = Array.from(map.values());

// The `mapValues` variable is now an Array containing ['Apple', 'Orange', 'Lemon']:
console.log(mapValues);

0

19.071

How to convert Map Values to an Array in JavaScript

JavaScript by Victor Talmacinschi June 21, 2022

// how to convert map values to an array in JavaScript
const myMap = new Map();

// setting map values
myMap.set('🍎', 'Apple');
myMap.set('🍌', 'Banana');
myMap.set('🍇', 'Grape');

// using the `Array.from()` method to convert map values to an array
const myArray = Array.from(myMap.values());

console.log(myArray); // ['Apple', 'Banana', 'Grape']

0

19.071

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories