'Get Only Values from a Map to an array
I want to get values of a map and store inside a string array in typescript.
myMap= [0:'Mohit',1:'Balesh',2:'Jatin'];
arr[];
Expected Result arr['Mohit','Balesh','Jatin']
Solution 1:[1]
First of all, your myMap
is not valid.
It should look like this:
myMap= {0:'Mohit',1:'Balesh',2:'Jatin'};
And getting an array from it should look like this:
myArr = Object.values(myMap);
Solution 2:[2]
To get an array from the values in a map you can spread the map values into an array using the Javascript spread operator (...) with the values() method of map.
yourArray = [...myMap.values()]
// gives the array ['Mohit', 'Balesh', 'Jatin']
Solution 3:[3]
Object[] values = hashMap.values().toArray();
// It will convert values into array of values
Solution 4:[4]
Map.values() returns a MapIterator object which can be converted to Array using Array.from:
let values = Array.from( myMap.values() );
// ["a", "b"]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Roberto Zvjerković |
Solution 2 | Fred Liebenberg |
Solution 3 | Siva Kumar |
Solution 4 | anilam |