Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created August 28, 2017 13:56
Show Gist options
  • Save prof3ssorSt3v3/3f98667c9be23e4ba1e2b71168007c22 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/3f98667c9be23e4ba1e2b71168007c22 to your computer and use it in GitHub Desktop.
// Maps vs Objects
// ES6 Maps are a good replacement for Objects
// in many circumstances but not all
let a = {'name': 'Sherlock'};
let b = {'name': 'Watson'};
let people = {};
people[a] = 'Detective'; // a ['object':Object]
people[b] = 'Doctor'; // b ['object':Object]
//can't use objects as keys for other objects
//object keys are converted into a string
console.log( people[a], people[b]);
let characters = new Map();
characters.set(a, 'Detective');
characters.set(b, 'Doctor');
console.log( characters.get(a), characters.get(b) );
for(let [key, value] of characters){
console.log(`${key} = ${value}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment