Lodash project - quick question on invert

Dear all,

I’m working on the Lodash project that is part of the full-stack curriculum. I wrote the following code to build the invert functionality. Somehow it is not working probably. What am I doing wrong? (The code looks similar to the one provided in the “Get unstuck” video, but I think I may be missing something crucial. Thanks a lot!

Best,
Gerrit

// _.invert()

_.invert = object => {

  let invertedObject = {};

  for(let key in object) {

    const originalValue = object[key];

    invertedObject = {originalValue : key}

  }

  return invertedObject;

}

this line:

invertedObject = {originalValue : key}

looks problematic to me. Instead of adding a key/value pair, a new object is stored in the variable, overwriting the value currently hold by the variable. Which means you will end up with an object with only a single key/value pair (the last one of the loop)

Super helpful, thanks! This is how I got it to work:

_.invert = object => {

let invertedObject = {};

for(let key in object) {

invertedObject[object[key]] = key

}

return invertedObject;

}

OMG I was struggling so hard to figure out the syntax to properly next the `invertedObject[object[key]] = key` portion of that code.