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

Victor Talmacinschi

How to convert Map Keys to an Array in JavaScript

JavaScript by Victor Talmacinschi June 21, 2022

const map = new Map();

map.set('🍎', 'Apple');
map.set('🍊', 'Orange');
map.set('🍇', 'Grape');

// To get an array of the keys:
const keys = Array.from(map.keys());

// To get an array of the values:
const values = Array.from(map.values());

// Or, if you don't need an array, you can iterate over the map entries:
for (const [key, value] of map) {
  console.log(key, value);
}

0

21.062

How to convert Map Keys to an Array in JavaScript

JavaScript by Victor Talmacinschi June 21, 2022

const map = new Map();

map.set('🍎', 'Apple');
map.set('🍐', 'Pear');
map.set('🍊', 'Orange');

// To get an array of the keys:
const keys = Array.from(map.keys());

// Log the array of keys
console.log(keys);
// ['🍎', '🍐', '🍊']

0

21.062

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories