0

I have been searching on how should new Map(); be used and how to assign values from both to the object or return the object.

I have found usage of new Map(); on the MDN webdocs which is:

let myMap = new Map();

myMap.set('key', 'value');

console.log(myMap.get('key'))

I have found in one of the posts it can be used as follows as well:

let myMap = new Map();

myMap['key'] = 'value';

console.log(myMap['key']);

I have used myMap['key'] way of of new Map() before but as i tried to return the map it seemed different.

This is example which shows difference between the two:

let myMap0 = new Map();

let myMap1 = new Map();

myMap0.set('key','value');

myMap1['key'] = 'value';

console.log(myMap0);

console.log(myMap1);

Given example doesn't look alike in Google Chrome which is: enter image description here

Overall this is really confusing for me as i am unsure which way is the way it is supposed to be used.

I got used to myMap['key'] = 'value'; as this is how it works in Golang.

Additionaly i have found more solutions to the myMap.set('key', 'value');way of usage.

3
  • Both are correct, just different implementations. Using the set method is probably more common because it is more readable, people could confuse your map with a simple array if you use the square bracket notation. Commented Apr 19, 2018 at 15:58
  • 4
    The set method is the correct way. Otherwise you’ll always have a size of 0 and no entries, as you see in the console. You’re just adding properties, but not entries. Commented Apr 19, 2018 at 15:58
  • Assigning to the Map object is very likely not what you want. Just use a normal object. Commented Apr 19, 2018 at 16:24

1 Answer 1

3

A Map has two distinct storage areas:

  1. The properties of the object (which all objects have)
  2. The entries of the Map

When you use myMap['key'] = 'value', you're assigning to the properties. When you use myMap.set('key', 'value'), you're assigning to the entries.

The entries of a Map are special because they don't require the keys to be strings, and since they're stored separately from the object properties, you don't have to worry about any name collisions with properties like toString or valueOf.

Map.prototype.set is the preferred way to set an entry in the Map, because it utilizes the features of the Map. Otherwise, if you just assign to the object's properties, you might as well be using a plain object instead.

Sign up to request clarification or add additional context in comments.

2 Comments

I have found Object.entries to work on 'myMap['key'] = 'value' (which behaves like an object) and for myMap.set('key', 'value') : myMap.entries() which is not so great.
@HowToGo [...myMap.entries()] or Array.from(myMap.entries())? Works just like Object.entries. With Array.from you can even provide a mapping function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.