We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Map vs. Object

In Go, developers use maps all the time. They're the only reasonable choice you're given for a dynamic key/value store.

In JavaScript, you have two options: objects and maps, and honestly, I still use objects more often than maps just because the syntax is so simple. That said, there are some advantages to using maps:

  1. Ordered: Map keys are ordered in an easy-to-understand way. Objects are not.
  2. Iterable: Maps are iterable, so you can use for (const [key, value] of myMap) to loop over them. Objects are not (without some extra work).
  3. Performance: Maps are typically faster when you need to do a lot of insertions and deletions.
  4. No extra properties: Maps don't have any extra built-in properties like __proto__ or constructor that you might not want.

All this to say, in most cases either will work for your KV needs, but it's important to understand some of the benefits of maps.

Assignment

Textio discovered a nasty bug: the intern creating the user map set objects (with fname and lname properties) as keys in our user map instead of string keys. Now we can't look up users... big problem!

Complete the fixUserMap function. It takes a Map with object keys mapping to user objects and returns a new fixed Map with string keys.

For example

const grossMap = new Map([
  [
    { fname: "Eren", lname: "Yeager" },
    { fname: "Eren", lname: "Yeager", tags: ["Survey Corps", "Titan Slayer"] },
  ],
]);
const fixedMap = fixUserMap(grossMap);
console.log(fixedMap);
// Map { 'Eren Yeager' => { fname: 'Eren', lname: 'Yeager', tags: [ 'Survey Corps', 'Titan Slayer' ] } }