Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Map keys to array?

Lets say I have the following map:

let myMap = new Map().set('a', 1).set('b', 2); 

And I want to obtain ['a', 'b'] based on the above. My current solution seems so long and horrible.

let myMap = new Map().set('a', 1).set('b', 2); let keys = []; for (let key of myMap)   keys.push(key); console.log(keys);

There must be a better way, no?

like image 345
Lilleman Avatar asked Feb 11 '16 14:02

Lilleman


People also ask

Can JavaScript map key Be array?

We can get the keys from the map object using the built-in method map. keys(). To convert map keys to an array we have to first initialize a map object, followed by using the map. keys() method and then use the spread operator or array.

Does map return an array?

The map() method returns an entirely new array with transformed elements and the same amount of data. In the case of forEach() , even if it returns undefined , it will mutate the original array with the callback .

How do you get all values of a map in JS?

entries() Method in JavaScript The Map. entries() method in JavaScript is used for returning an iterator object which contains all the [key, value] pairs of each element of the map. It returns the [key, value] pairs of all the elements of a map in the order of their insertion.

How to convert map keys to an array in JavaScript?

How to convert Map keys to an array in JavaScript ? Given a Map and the task is to get the keys of the map into an array using JavaScript. Use keys () method on Map Object to get the keys of Map. Then use the array.from () method to convert map object to array.

How to convert a hashmap to an array of keys?

Object[]objectArray=hashMap.entrySet().toArray(); System.out.println(Arrays.toString(objectArray)); Download  Run Code Output: [RED=#FF0000, BLUE=#0000FF] 2. Convert map to array of keys We can get an array of keys of the map using Map.keySet()with Set.toArray(T[] a).

How to get all the keys of a map in Java?

Call the keys () method on the Map to get an iterator object that contains all of the keys in the Map. Call the Array.from () method, passing it the iterator as a parameter. The Array.from method creates a new array from an iterable object. Copied! We used the Map.keys method to get an iterator object containing the keys of the Map.

How to get an array of key-value pairs from a map?

We have seen that we can get an array of keys and values of the map using Map.keySet()and Map.values(), respectively. We can easily construct an array of key-value pairs from key[]and value[].


2 Answers

Map.keys() returns a MapIterator object which can be converted to Array using Array.from:

let keys = Array.from( myMap.keys() ); // ["a", "b"] 

EDIT: you can also convert iterable object to array using spread syntax

let keys =[ ...myMap.keys() ]; // ["a", "b"] 
like image 191
pawel Avatar answered Sep 22 '22 06:09

pawel


You can use the spread operator to convert Map.keys() iterator in an Array.

let myMap = new Map().set('a', 1).set('b', 2).set(983, true)  let keys = [...myMap.keys()]  console.log(keys)
like image 31
Fernando Carvajal Avatar answered Sep 24 '22 06:09

Fernando Carvajal