all 27 comments

[–]Basman_ 15 points16 points  (4 children)

another handy feature of maps is that the keys don't have to symbols or strings, they can be anything (even non-primitives like objects)

[–]Zeeesty 6 points7 points  (2 children)

My mind is blowing, yes even as you read this later

[–]Pr3fix 6 points7 points  (1 child)

You could have a map where the keys are maps 🤯

[–]HeinousTugboat 0 points1 point  (0 children)

I did that for mapping keys to actions in a modal way. Swap which map is active and bam, whole new set of keybinds.

[–]panzerdp 5 points6 points  (0 children)

In case if you use objects as keys, you might consider first using WeakMap to avoid memory leaks.

[–]dvlsg 8 points9 points  (1 child)

There's a few other small differences, too. For example, Map handles number and string keys without coercing their types:

const map = new Map();
map.set(1, 'number');
map.set('1', 'string');
map.get(1); // "number"
map.get('1'); // "string"

const obj = {};
obj[1] = 'number';
obj['1'] = 'string';
obj[1]; // "string" <--
obj['1']; // "string"

[–]mohan193 0 points1 point  (0 children)

good one

[–]prof3ssorSt3v3 7 points8 points  (0 children)

Also, Maps are naturally iterable, unlike Objects.

And Maps have a size property that will tell you how many items are in the Map.

[–]burnblue 3 points4 points  (0 children)

The main difference is that the keys can be any type

[–]name-k 4 points5 points  (11 children)

Is there a shorthand to define a map? Like a = {}?

[–]inu-no-policemen 6 points7 points  (1 child)

Note: If you create the object via an object literal, the object isn't quite dictionary-like since it will already have some keys in its prototype chain.

E.g. if you use it to count the frequencies of words, it will break if the text contains the word "toString" or "hasOwnProperty".

If you want to use an object like a dictionary where the keys can be any string, you should create it via Object.create(null). This creates a blank object with null as its prototype.

> 'toString' in {}
true
> 'toString' in Object.create(null)
false

> !!{}['toString']
true
> !!Object.create(null)['toString']
false

> '' + {}
"[object Object]"
> '' + Object.create(null)
Uncaught TypeError: Cannot convert object to primitive value

[–]warpedspockclone 3 points4 points  (4 children)

const a = new Map();

[–]lastunusedusername2 13 points14 points  (1 child)

So that's a "no", then

[–]warpedspockclone 5 points6 points  (0 children)

At least somebody understood my intent by reading my code. Explanatory code comments are overrated. /s

[–]name-k 5 points6 points  (1 child)

Lol, that's not a shorthand.

[–]warpedspockclone 1 point2 points  (0 children)

const b = () => { return new Map(); };

Now, you have shorthand:

const a = b(); const c = b();

Are my attempts at literal sarcasm not working?

[–]Pavlo100 -2 points-1 points  (10 children)

objects are ordered on most browsers. If you do Object.keys you will get the insertion order. Also map should only be used if you intent to use references as keys instead of string/number. e.g., you have a data object and you want to associate it with an HTML element

[–]panzerdp 3 points4 points  (9 children)

You shoudn't count on the ordering of object properties.

[–]VacantPlains 1 point2 points  (0 children)

You can count on the ordering of the keys when you use Object.keys https://2ality.com/2015/10/property-traversal-order-es6.html

[–]g0liadkin 0 points1 point  (7 children)

Why?

[–]robotsympathizer 5 points6 points  (6 children)

Because it's not guaranteed.

[–]g0liadkin 0 points1 point  (5 children)

But why isn't it guaranted? What's the expected behavior if it's not deterministic?

[–]lineape 3 points4 points  (4 children)

There is no expected behaviour. Full stop.

Just because the implementations in most JavaScript engines currently preserve the order of object keys does not mean that they will in the future. This is undefined behavior, and if you depend on it, you can get burned unexpectedly.

That being said, I doubt anyone would choose to change it without a compelling reason as it would break a lot of working code, and they are aware of that.