skip to Main Content

Hi guys I’ve been learning about Map in JavaScript and how we can use it instead of object in some practical situations.

In my learning journey I’ve found in JavaScript the Definitive Guide – David Flanagan that he mentions Map are, quote:

Remember, though, that a map is a set of keys, each of which has an associated value. This is not quite the same as a set of key/value pairs. If you call set() with a key that already exists in the map, you will change the value associated with that key, not add a new key/value mapping

But then I go to MDN and they’re saying the opposite, so what do I follow or believe?, also, isn’t there like an official technical definitio for this?, I have to check out EcmaScript 6…

Note: at the moment of writing this post I’ve found in the official ES6 documentation that indeed MDN is right, Map are a set of key/value pairs… Why did I spent my money on David Flanagan smh

enter image description here

Anyway I’ll leave the question open in case there is something I’m missing, thank you in advance.

2

Answers


  1. I’d say that it is more language question than technical. Because overall they all right. MDN says as usually technickal documentation says. At the same time the quote you provided tries to emphasize that for the map unique is only key and not key/value. I think this way he tried to show differense between Map.set('a', 1) and Array.push({a: 1}).

    Login or Signup to reply.
  2. No. A Map is neither a Set of keys, nor a Set of key-value pairs.

    As you found in the specification, a Map is a collection, and it contains key-value pairs where the keys must be distinct.

    […] a set of keys, each of which has an associated value. This is not quite the same as a set of key/value pairs.

    That’s true. A set is a collection where the elements are distinct. Every element is unique, there can be no duplicates. Describing a Map as a "set of key-value pairs" would be incorrect, as that would mean the whole pairs must be distinct, i.e. there could exist multiple pairs in the set with the same key but different values.

    However, I think it’s incorrect to say that a map is a set of anything. Rather, a map has a set of keys, and it also has an associated value for each of those keys.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search