Don't use object properties

// ❌ wrong use of Map
const wrongMap = new Map();
wrongMap['key1'] = 'value1';    // add "object properties"❗️
wrongMap['key2'] = 'value2';

// ✅ correct use of Map
const dict = new Map();
dict.set('Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"});
dict.set('Hilary', {phone: "617-555-4321", address: "321 S 2nd St"});

// --------------- log ---------------

[
  wrongMap,                 // {key1: 'value1', key2: 'value2'} ⭐️
  wrongMap.has('key1'),     // false❗️
  wrongMap.delete('key2'),  // false❗️ (no such key)
  '---------------------',
  
  dict,                     // {} (object Map) ⭐️
  dict.has('Jessie'),       // true
  dict.delete('Raymond'),   // false (no such key)
  dict.delete('Hilary'),    // true  (key deleted successfully)
  dict.get('Hilary'),       // undefined (no such key)
  dict.get('Jessie'),       // {phone: "213-555-1234", address: "123 N 1st Ave"}
  
  dict.size,                // 1
  
].forEach(x => log(x));

Last updated