+ 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?

5th Apr 2018, 6:35 AM
Vladi Petrov
Vladi Petrov - avatar
4 Answers
+ 7
wow, how many challenges 😂💪I will try to win....tomorrow ))
6th Apr 2018, 11:20 PM
🕸carpe diem🕸
🕸carpe diem🕸 - avatar
+ 6
Good question👍I didn't understand too
6th Apr 2018, 1:40 PM
🕸carpe diem🕸
🕸carpe diem🕸 - avatar
+ 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.
5th Apr 2018, 6:41 AM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 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.
5th Apr 2018, 7:20 AM
Mike Choy
Mike Choy - avatar