Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JavaScript Map values as array?

Is there like a quick way to get Javascript Map values as an array?

const map:Map<number, string> = new Map()
map.set(1, '1')
map.set(2, '2')

And then something like Array.from(map.values()) would give ['1','2'] ... I could have sworn that I've something like this...?

like image 993
Ole Avatar asked Dec 06 '25 17:12

Ole


2 Answers

It is working fine. Array.from can take an iterable value as well.

const map = new Map;

map.set(1, '1');
map.set(2, '2');

console.log(Array.from(map.values()));
like image 160
Nina Scholz Avatar answered Dec 08 '25 08:12

Nina Scholz


Another alternative could be using Spread syntax (...).

const map = new Map().set(4, '4').set(2, '2');
console.log([...map.values()]);
like image 38
Penny Liu Avatar answered Dec 08 '25 08:12

Penny Liu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!