Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat all values inside a map

I've got a constant result like this :

result: Map<string, string[]>

When I do a console.log(result) the output is :

Map {
 'toto' => [ 'a-1', 'a-2' ],
 'tata' => [ 'b-1', 'b-2' ],
 'titi' => [ 'c-1', 'c-2' ],
}

What I want to have, it's a constant globalResult with all values like this:

const globalResult = [ 'a-1', 'a-2','b-1','b-2','c-1','c-2' ]

How can I do this ?

Thanks

like image 677
azdaj zdnakjdnaz Avatar asked May 10 '26 07:05

azdaj zdnakjdnaz


1 Answers

You can get map values into an array and then use flat() method on it like:

const myMap = new Map().set('toto', ['a-1', 'a-2']).set('tata', ['b-1', 'b-2'])
const arr = [...myMap.values()].flat()

console.log(arr)
like image 157
palaѕн Avatar answered May 12 '26 20:05

palaѕн



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!