Map

Note: No typecasting in keys.

Creating Maps

Normal way

  1. const map = new Map();
  2. map.set('name', 'Jorge');
  3. map.get('name'); // Jorge
  4. map.has('name'); // true

With Arrays

  1. var kvArray = [["key1", "value1"], ["key2", "value2"]];
  2. // Use the regular Map constructor to transform a 2D key-value Array into a map
  3. var myMap = new Map(kvArray);
  4. myMap.get("key1"); // returns "value1"

Objects as keys

The key can be a function, a primitive, an object..
But it has to be exactly the same. If it is a copy or it is mutated, then it will stop working.

  1. const user = { name: 'Jorge', id: 1234 };
  2. const userHobbyMap = new Map();
  3. userHobbyMap.set(user, ['Ice Fishing', 'Family Outting']);