Home > Back-end >  confusing behaviour of Map<string, string[]> = new Map()
confusing behaviour of Map<string, string[]> = new Map()

Time:01-26

I was trying to insert some value into a map in my angular project. Map is initialized with below code:

filters: Map<string, string[]> = new Map();

But When I insert some value into this above map is behaving unusual.First Image with Map(1)

sometimes it is giving me the same Map with different value. Look

Second Image with Map(2)

First image is showing Map(1) and the second image is showing Map(2).

I need second type of map after inserting a value. Could anyone can help me out what is the difference between those twos? I am totally stuck for this. Badly need some help. Many Many thanks in advance.

CodePudding user response:

your code works fine. it is the devtools feature. At the moment you log your map, you are logging the link to a map, where you can expand it and you will see the actual value (that is why you see the same expanded view in both of the screenshots).

Devtools try to help you a bit and make a string which could help you find your map in the console. But at the moment of logging 2nd item is not yet in the map. Because of the item is not in the map that helper string is not the latest one.

CodePudding user response:

Dev tools is showing Map(1) in the console output due to there being only one value present at the time of logging, then it shows Map(2) at the second time of logging.

Map is a reference to an object in dev tools, so when you inspect the Map value by expanding it in dev tools, you will see it's current value.

Here is an example of this in action

const map = new Map();

map.set("a", ["a"]);

console.log(map); // Shows Map(1) as map has one entry

map.set("b", ["a", "b"]);

console.log(map); // Shows Map(2) as map has two entries

  •  Tags:  
  • Related