+ 3
Javascript Map
Can someone explain me why this code from challenge outputs 1. Code: let map = new Map([['a', '1'], ['a', '1'], ['a', '2']]); console.log(map.size); I know that map keeps only unique value - with different hashcode, but how ['a', '1'] and ['a', '2'] can have same hashcode?
4 odpowiedzi
+ 7
wow, how many challenges 😂💪I will try to win....tomorrow ))
+ 6
Good question👍I didn't understand too
+ 3
Map is like a dictionary: one key for one value. There is only a->2 in map. Use Set instead of map to have unique pairs.
+ 3
Hi Vladi
as @Bartosz says Map should have uniques keys, so what is happening is that each subsequent declaration of "a" overwrites the previous declaration, so your map becomes Map([a,2]) which has length of 1.
If you do a console.log(map.get('a')) you will see output is 2 which is the last declaration.