Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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']

like image 384
Mohit Sharma Avatar asked Dec 22 '22 21:12

Mohit Sharma


2 Answers

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);
like image 128
Roberto Zvjerković Avatar answered Dec 28 '22 06:12

Roberto Zvjerković


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']
like image 35
Fred Liebenberg Avatar answered Dec 28 '22 07:12

Fred Liebenberg