Friday 2 June 2017

Javascript/NodeJS, get the length of a map

so I have a small problem returning the size of a map :

    var siteRoomMap = new Map;
    ............... (algorithm to fill the map)

    console.log('in start');
    console.log(siteRoomsMap);
    console.log(siteRoomsMap.size);

and this is what i get in the terminal :

    in start
 { '1': 
  { siteNumber: 1,
    roomsMap: { '1': [Object], '2': [Object], '3': [Object] },
    siteState: false },
   '2': { siteNumber: 2, roomsMap: { '1': [Object] }, siteState: false } }
 0

I mean, my map clearly has 2 keys/values in it, but it always returns 0. I tried map.length but I get an undefined (probably cause .length doesn't exist for a map ? )

I could write a function my self that would look like that and call it on my map with

    size = myfunction(siteRoomsMap);

    myfunction function(data){
    for (i in data) {
    size++}
    return size

but why can't I use .size ? I looked in the doc and it clearly says it returns the number of element in the map ???



via Sech

No comments:

Post a Comment