Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get length of TypeScript Map<k, v>?(Error: Map.size gives undefined)

I have Map in my angular component as key being object of class Group and value being an array of class Match which basically says group and matches that will take place among the group members of that group. .

Map< Group, Match[]>

I want to have the total number of entries in the map and length of match array as well. How do I do that?

I've tried map.size and map.values().next().length

like image 481
Parth Savaliya Avatar asked Oct 16 '25 16:10

Parth Savaliya


2 Answers

Sometimes newMap.size gives undefined value. Use this simple code to find map size.

Object.keys(newMap).length 
like image 82
Sathia Avatar answered Oct 19 '25 07:10

Sathia


I encountered a similar problem. You have to call the Map constructor. My original code was:

 let a : Map<string,number> = {} as Map<string,number>;
 Map.size //undefined

If you call it properly, then works:

let a: Map<string,number> = new Map<string,number>();
Map.size //the right size
like image 22
Ivasan Avatar answered Oct 19 '25 05:10

Ivasan



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!